ClassNotFoundException: org.jboss.naming.remote.cl

2019-07-12 03:55发布

问题:

Environment used:

  • WildFly 9.0.2.Final
  • EJB 3.0
  • Ant
  • Eclipse

Test.jsp is invoking getDbConnection method of Test.java class.

Test.jsp

<%@page import="com.testmodule.pojo.Test"%>
<%
    try{
        System.out.println(" Going to call getDbConnection method of 'Test' class. This 'Test' class shall be "
            + " part of testclient.jar. testclient.jar shall be deployed as a module --- module add --name=testclient --resources=/Downloads/lib/test/testclient.jar");
        System.out.println(" test.jsp shall be present in test.ear...'jboss-deployment-structure.xml' shall be present in "
            + " in META-INF directory of test.ear with entry as -- <dependencies><module name=\"testclient\" export=\"true\" /> </dependencies>");
        System.out.println(" Dont want to put 'jboss-client.jar' in Test.ear-->Test.war-->WEB-INF/lib , As there are multiple ear's which are accessing Test.java class");

        new Test().getDbConnection();
    } catch(Exception e) {
        e.printStackTrace();
    }
%>

Test.java

package com.testmodule.pojo;

import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;

public class Test { 
    public void getDbConnection(){
        try {
            Properties jndiProps = new Properties();

            jndiProps.put(Context.INITIAL_CONTEXT_FACTORY, 
                "org.jboss.naming.remote.client.InitialContextFactory");
            jndiProps.put(Context.PROVIDER_URL,"remote://29.86.30.73:4447");
            jndiProps.put(Context.SECURITY_PRINCIPAL, "testuser");
            jndiProps.put(Context.SECURITY_CREDENTIALS, "testpassword");
            jndiProps.put("jboss.naming.client.ejb.context", true);

            Context context = new InitialContext(jndiProps);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}

Following error occurred while accessing test.jsp.

10:09:26,536 ERROR [stderr] (default task-1) javax.naming.NamingException: WFLYNAM0027: Failed instantiate InitialContextFactory org.jboss.naming.remote.client.InitialContextFactory from classloader ModuleClassLoader for Module "deployment.test.ear.test.war:main" from Service Module Loader [Root exception is java.lang.ClassNotFoundException: org.jboss.naming.remote.client.InitialContextFactory from [Module "deployment.test.ear.test.war:main" from Service Module Loader]]
10:09:26,537 ERROR [stderr] (default task-1)    at org.jboss.as.naming.InitialContext.getDefaultInitCtx(InitialContext.java:118)
10:09:26,537 ERROR [stderr] (default task-1)    at org.jboss.as.naming.InitialContext.init(InitialContext.java:99)
10:09:26,537 ERROR [stderr] (default task-1)    at javax.naming.ldap.InitialLdapContext.<init>(InitialLdapContext.java:154)
10:09:26,537 ERROR [stderr] (default task-1)    at org.jboss.as.naming.InitialContext.<init>(InitialContext.java:89)
10:09:26,548 ERROR [stderr] (default task-1) Caused by: java.lang.ClassNotFoundException: org.jboss.naming.remote.client.InitialContextFactory from [Module "deployment.test.ear.test.war:main" from Service Module Loader]
10:09:26,548 ERROR [stderr] (default task-1)    at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:205)
10:09:26,548 ERROR [stderr] (default task-1)    at org.jboss.modules.ConcurrentClassLoader.performLoadClassUnchecked(ConcurrentClassLoader.java:455)
10:09:26,549 ERROR [stderr] (default task-1)    at org.jboss.modules.ConcurrentClassLoader.performLoadClassChecked(ConcurrentClassLoader.java:404)
10:09:26,549 ERROR [stderr] (default task-1)    at org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:385)

I don't want to put jboss-client.jar in Test.ear --> Test.war --> WEB-INF/lib, as there are multiple EARs which will access Test.java class.

回答1:

Your JNDI code is for remote call while you are trying to load some local datasource. You may add a dependency on the jboss client module with a jboss-deployment-structure.xml file.