This one is starting to go out completely ...
I want to create a Glassfish client application using Maven.
For this, I added the requried gf client dependency:
<dependency> <groupId>org.glassfish.appclient</groupId> <artifactId>gf-client</artifactId> <version>3.1</version> <type>pom</type> <scope>compile</scope> </dependency>
Then, wanting to contact my Glassfish server while working in one application, I do a normal search:
Properties p = new Properties(); // optional. Defaults to localhost. Only needed if web server is running // on a different host than the appserver p.setProperty("org.omg.CORBA.ORBInitialHost", "localhost"); // optional. Defaults to 3700. Only needed if target orb port is not // 3700. p.setProperty("org.omg.CORBA.ORBInitialPort", "3700"); Context context = new InitialContext(p); // Stores the list of reachable EJBs return context.lookup(interfacesToNames.getProperty(className));
Unfortunately, with this, all I get is
Caused by: javax.naming.NoInitialContextException: Cannot instantiate class: com.sun.enterprise.naming.impl.SerialInitContextFactory [Root exception is java.lang.ClassNotFoundException: com/sun/enterprise/naming/impl/SerialInitContextFactory] at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:674) at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307) at javax.naming.InitialContext.init(InitialContext.java:242) at javax.naming.InitialContext.<init>(InitialContext.java:216) ... 6 more Caused by: java.lang.ClassNotFoundException: com/sun/enterprise/naming/impl/SerialInitContextFactory at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:264) at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:63) at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:671) ... 9 more
After some checks, I noticed that my glassfish-naming-3.1.jar present in CLASSPATH for this client application. And, according to the Eclipse code search, this particular jar should contain com.sun.enterprise.naming.impl.SerialInitContextFactory . However, if in debug mode I do getClass().getClassLoader().getResource("com/sun/enterprise/naming/impl/SerialInitContextFactory") , it returns me null, which clearly indicates that the class cannot be found.
For more information, the JAR is copied from my local maven repository using this plugin configuration:
<plugin> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>package output directory</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <prependGroupId>true</prependGroupId> <includeScope>compile</includeScope> <outputDirectory>${dependencies.outputDir}/jars</outputDirectory> <prefix>${dependencies.outputDir}</prefix> </configuration> </execution> </executions> </plugin>
In addition, I must admit that when running a simple test using the same API to connect to the Glassfish server, there is absolutely no problem that directs me to the ClassLoader problem.
When this client starts, the current class loader (as getClass().getClassLoader().getClass().getName() ) sun.misc.Launcher$AppClassLoader . Unfortunately, this is exactly the same as when running unit test.
So what can I do to solve this error?
EDIT The class exists in glassfish-naming-3.1.jar , but does not seem to be found by the regular class loader.
EDIT Interesting discovery:
getClass().getClassLoader().getResource("com/sun/enterprise/naming/impl") = jar:file:/C:/Users/pouet/pouet/target/jars/glassfish-naming-3.1.jar!/com/sun/enterprise/naming/impl
while
getClass().getClassLoader().getResource("com/sun/enterprise/naming/impl/SerialInitContextFactory") = null