Why Doesn't Standalone Java Client Get InitialContext from localhost WildFly 8?
gberish Nov 7, 2015 5:55 PMI am a beginner escaping from Glassfish to WildFly. And failing miserably. So at this point any help would be welcome.
My key need is getting a Java client to swap JMS messages with a JEE app deployed on WildFly. Ultimate client will be a Java Swing GUI launched by Browser via WebStart(JNPL). For now its the java program below running on Eclipse Mars. Wildfly 8 is running on local host. All are on same machine.
Step 1 is to get a JMS ConnectionFactory. Before Oracle messed up JMS in GF4, that was a trivial lookup(). Per WildFly documentation (always a sketchy thing) I thought it would still be easy.
https://docs.jboss.org/author/display/WFLY8/Messaging+configuration
"Since JMS 2.0, a default JMS connection factory is accessible to EE application under the JNDI name java:comp/DefaultJMSConnectionFactory."
Two weeks later: Many questions posted to many sites. No solution. I'm nowhere.
So here's the code as simplified as I can make it. The comments keep track of jars various people have said are needed. Any insight, help, direction, or suggestions (other than quit) would be welcome. (Note: The client does not run in a JEE container, so nothing that involves "injection" is applicable.)
I'm obviously missing something fundamental. I mean I can't even kill the @!&@%$! unrelated, unneeded, Derby warning.
And the ClassNotFound org.jboss.as.naming.InitialContextFactory.class
makes no sense to me. See imports where I even imported it to be sure it was in the build path
CLIENT CODE:
package org.america3.wildfly.jms;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
import com.sun.messaging.ConnectionFactory;
import org.jboss.as.naming.InitialContextFactory;
public class JMSTest1 {
static Hashtable<String, String> ENV = new Hashtable<String,String>() {
private static final long serialVersionUID = 1L;
{
put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.as.naming.InitialContextFactory.class");
/*
* InitialContextFactory is in wildfly-naming-9.0.2.Final.jar
* WildFly stores the jar in <WILDFLY_HOME>\modules\system\layers\base\org\jboss\as\naming\main
* I added it to the Eclipse build path
*/
put(Context.PROVIDER_URL, "localhost:8080");
}
};
static public void main (String[] args) {
try {
final Context context = new InitialContext(ENV);
ConnectionFactory factory = (ConnectionFactory) context.lookup("java:comp/DefaultJMSConnectionFactory");
System.out.println(factory);
} catch (Exception e) {
System.out.println("MY EXCEPTION CATCH");
System.out.println("Name: " + e.getClass().getName());
System.out.println("Message: " + e.getMessage());
e.printStackTrace();
}
}
/*
* This is a list of jar files added to WildFly and/or the Eclipse
* build path
*
* derbyclient.jar and derby-driver.jar are stored in these files:
* <WILDFLY_HOME>\standalone\deployments
* <WILDFLY_HOME>\standalone\deployments\lib
* C:\Program Files\Java\jdk1.8.0_05\lib
* C:\Program Files\Java\jdk1.8.0_05\jre\lib
*
* Added the entire Derby API and derby-driver.jar to the Eclipse
* build path for this class.
*
* Also added these jars from <WILDFLY_HOME>\modules\system\layers\base\
* to Eclipse build path
* jboss-common-core-2.5.0.Final.jar
* jboss-ejb3-ext-api-2.1.0.jar
* jboss-ejb-client-2.1.1.Final.jar
* jboss-iiop-client-1.0.0.Final.jar
* wildfly-appclient-9.0.2.Final.jar
* wildfly-naming-9.0.2.Final.jar
* hornetq-core-client-2.4.7.Final.jar
* hornetq-jms-client-2.4.7.Final.jar
*/
}
CONSOLE OUTPUT:
MY EXCEPTION CATCH
Name: javax.naming.NoInitialContextException
Message: Cannot instantiate class: org.jboss.as.naming.InitialContextFactory.class
- javax.naming.NoInitialContextException: Cannot instantiate class: org.jboss.as.naming.InitialContextFactory.class [Root exception is java.lang.ClassNotFoundException: org.jboss.as.naming.InitialContextFactory.class]
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.init(Unknown Source)
at javax.naming.InitialContext.<init>(Unknown Source)
at org.america3.wildfly.jms.JMSTest1.main(JMSTest1.java:27)
Caused by: java.lang.ClassNotFoundException: org.jboss.as.naming.InitialContextFactory.class
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at com.sun.naming.internal.VersionHelper12.loadClass(Unknown Source)
at com.sun.naming.internal.VersionHelper12.loadClass(Unknown Source)
... 5 more