使用的环境:
Test.jsp 正在调用 Test.java 类的getDbConnection
方法。
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();
}
}
}
访问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)
我不想在Test.ear中放置 jboss-client.jar - &gt; Test.war - &gt; WEB-INF / lib,因为有多个EAR将访问 Test.java 类。