Thursday, October 22, 2015

org.jboss.naming.remote.client.InitialContextFactory was not found in JBoss server runtime

The same piece of code, execute on different PCs, I'll have different results.
 Properties props = new Properties();
 props.put("java.naming.factory.url.pkgs", "org.jboss.ejb.client.naming");
 props.put("java.naming.factory.initial", "org.jboss.naming.remote.client.InitialContextFactory");
 props.put("java.naming.provider.url", "remote://127.0.0.1:4447");
 props.put("jboss.naming.client.ejb.context", "true");
 props.put("jboss.naming.client.connect.options.org.xnio.Options.SASL_POLICY_NOPLAINTEXT","false");

 InitialContext context = new InitialContext(props);

The above code was compiled and execute successfully without error. But when I move the piece to another PC, the compilation will fail and following error would be seen.
Caused by: java.lang.ClassNotFoundException: org.jboss.naming.remote.client.InitialContextFactory
 at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
 at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
 at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
 at java.lang.Class.forName0(Native Method)
 at java.lang.Class.forName(Class.java:274)
 at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:72)
 at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:61)
 at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:671)
 ... 4 more
Although I do have already configured JBoss EAP 6 as the server runtime in my project configuration, unfortunately none of the JARs found the org.jboss.naming.remote.client.InitialContextFactory class. Actually, this class can be found in jboss-client.jar and it was so unlucky that it has been excluded being a member of Jboss server runtime. Sigh... Thus, I have to explicitly include this JAR into the classpath and it is easily found in <jboss_root>\bin\client.

No comments: