How to setup JNDI for Glassfish 3.1.2 for a stand-alone client?
Asked Answered
M

3

3

I try to connect from a stand-alone swing client (running in a separate JVM on the client machine) to the Glassfish server.

I currently use the following settings from Netbeans and everything works just fine:

System.setProperty("java.naming.factory.initial", "com.sun.enterprise.naming.SerialInitContextFactory");
System.setProperty("java.naming.factory.url.pkgs", "com.sun.enterprise.naming");
System.setProperty("java.naming.factory.state", "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
System.setProperty("org.omg.CORBA.ORBInitialHost", "192.168.1.3");
System.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
InitialContext context = new InitialContext();

But when I try to start the compiled client from the console by typing "java -jar client.jar" I get the following error:

D:\workspace\gf-client\dist>java -jar gf-client.jar
17.08.2012 11:07:38 ch.client.core.ServerContext getInitialContext SCHWERWIEGEND: Cannot instantiate class: com.sun.enterprise.naming.SerialInitContextFactory javax.naming.NoInitialContextException: Cannot instantiate class: com.sun.enterprise.naming.SerialInitContextFactory [Root exception is java.lang.ClassNotFoundException: com.sun.enterprise.naming.SerialInitContextFactory]
        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 ch.lawsuite.core.ServerContext.getInitialContext(ServerContext.java:2 7)
        at ch.client.core.remote.Facades.initialize(Facades.java:68)
        at ch.client.core.Client.main(Client.java:57) Caused by: java.lang.ClassNotFoundException: com.sun.enterprise.naming.SerialIni tContextFactory
        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)
        ... 7 more Exception in thread "main" java.lang.NullPointerException
        at ch.client.core.remote.Facades.initialize(Facades.java:69)
        at ch.client.core.Client.main(Client.java:57)

Somebody any helpful idea?

  • Does the JVM miss any libs? Which ones? (it works from netbeans and all dependent libs are packed to the compiled jar-file (at least I think so..))
  • Are there alternative serial context factories for Glassfish ?

Many thanks for your help in advance!

Myron answered 17/8, 2012 at 9:11 Comment(2)
Maybe this can help you: #7804857Porous
I would run with appclient from glassfish, and, if necessary, make a script to launch it.Portuguese
M
1

By looking into the MANIFEST.MF file of the gf-client.jar library I noticed that there are some dozen other jar-libs referenced from there. In order to run the client outside of the netbeans platform, I had to copy all these libs to the final build of my own application. Then it works just fine... :-)

Myron answered 24/10, 2012 at 8:30 Comment(4)
and you just include those jars in a /lib folder with the app?Portuguese
no, it is very important that the structure of all these glassfish libs is kept up! so you have to add the gf-client.jar to your libs relatively to the glassfish installation directory. that means: leave gf-client.jar in glassfish/lib/gf-client.jar and just point to that jar from within your project. if you want to run your app outside the IDE (as stand-alone), make sure that the glassfish folder is referenced the same way as before. if you need more information, let me know...Myron
hmm, interesting. gf-client is the primary requirement?Portuguese
yes. add gf-client.jar as first lib, otherwise you will run into name conflicts with other jars (I think it was java-ee-7.jar).Myron
I
8

A point to clear about Remote EJB interfaces

You want a Remote EJB interface when your client application lives on one JVM different than the one hosting the EJB module. In other words:

  • The client application lives on one JVM and the EJB module is deployed on another JVM on the same machine

OR

  • The client application lives on one JVM and the EJB module is deployed on another JVM on a remote machine.

For simplicity sake

Lets consider the first scenario where both of the client app and the EJB module live on different JVMs on the very same machine.

  1. Make sure you have 2 JDKs installed on your machine.
  2. Deploy the EJB module in a Glassfish installation pointing to one JDK (JVM). We can agree to install Glassfish in "C:/glassfish3/"
  3. According to the documantation in this link. Add to your client application classpath the file "gf-client.jar" as an external library from within the installation directory (i.e. C:/glassfish3/glassfish/lib/gf-client.jar) rather than copying it.
  4. Also add to your client application classpath the file remote_interface.jar includes the Remote business interface of your EJB.
  5. Run the SE (stand alone) client application on the second JDK (JVM)

An important tip about the client

As per the documentation, stand-alone java clients must explicitly use the global JNDI name to lookup the Remote EJB. Also, Glassfish does not need any properties instialization to invoke the InitialContext() constructor. Thus the client application can invoke the EJB using the following snippet:

InitialContext context = new InitialContext();
_RemoteEjbInterface ejbBean = (_RemoteEjbInterface) context.lookup("java:global/DeployedEJBAppName/EjbImplClass!com.sam._RemoteEjbInterface");

In case your SE stand alone client application is a maven one then you need to

  1. Account for the above step (3) by adding this entry to your client app POM:

    <dependency>
        <groupId>org.glassfish.main.appclient.client</groupId>
        <artifactId>gf-client</artifactId>
        <version>3.1.2</version>
        <scope>system</scope>
        <systemPath>C:/glassfish3/glassfish/lib/gf-client.jar</systemPath>
    </dependency>
    
  2. Account for the above step (4) by a POM dependency pointing to you remote_interface.jar in your local maven repo assuming you installed it. Follow this to know how.

Another documentation to reference is here

Illdisposed answered 27/9, 2013 at 16:14 Comment(2)
does the global JNDI name change depending on whether the EAR is deployed to localhost or to a remote server? I would, naively, expect that the EAR will specify the exact global JNDI name explicitly...?Portuguese
saved my life. 3 weeks of debuging.Neille
M
1

By looking into the MANIFEST.MF file of the gf-client.jar library I noticed that there are some dozen other jar-libs referenced from there. In order to run the client outside of the netbeans platform, I had to copy all these libs to the final build of my own application. Then it works just fine... :-)

Myron answered 24/10, 2012 at 8:30 Comment(4)
and you just include those jars in a /lib folder with the app?Portuguese
no, it is very important that the structure of all these glassfish libs is kept up! so you have to add the gf-client.jar to your libs relatively to the glassfish installation directory. that means: leave gf-client.jar in glassfish/lib/gf-client.jar and just point to that jar from within your project. if you want to run your app outside the IDE (as stand-alone), make sure that the glassfish folder is referenced the same way as before. if you need more information, let me know...Myron
hmm, interesting. gf-client is the primary requirement?Portuguese
yes. add gf-client.jar as first lib, otherwise you will run into name conflicts with other jars (I think it was java-ee-7.jar).Myron
C
0

You are missing the jar containing the class com.sun.enterprise.naming.SerialInitialContextFactory in your classpath. Add it to the manifest of your client jar.

Custos answered 17/8, 2012 at 9:45 Comment(1)
I know that this class is within appserv-rt.jar which in turn is referenced by gf-client.jar's Manifest-file which is actually on my classpath. How can I tell Netbeans to include libs which are referenced by the Manifest-file of included libs?Myron

© 2022 - 2024 — McMap. All rights reserved.