Skip to main content

EJB Client runs in Netbeans, but not outside

12 replies [Last post]
janmueller
Offline
Joined: 2008-02-20

Hello

I developed a simple EJB-Client, which I want to run unmanaged.

So I searched the web (and I looked at the EJB-FAQ ;-)) and came up with this:

InitialContext ic = new InitialContext();
auftragsabwicklungBean = (AuftragsabwicklungRemote) ic.lookup("beans.AuftragsabwicklungRemote");

Runs fine in netbeans, but If I run from the console I get the Exception:

javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial

So finally I looked into the appserv-rt.jar and changed my code to:

Properties p = new Properties(); p.put("java.naming.factory.initial","com.sun.enterprise.naming.SerialInitContextFactory"); p.put("java.naming.factory.url.pkgs","com.sun.enterprise.naming"); p.put("java.naming.factory.state","com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
InitialContext ic = new InitialContext(p);

Runs still fine from Netbeans, but on the console I get:

javax.naming.NoInitialContextException: Cannot instantiate class: com.sun.enterprise.naming.SerialInitContextFactory [Root exception is java.lang.ClassNotFoundException: 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]

Has someone a clue what I sould do?

Greetings,
Jan

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
mdynca
Offline
Joined: 2010-07-11

I had the same problem (NetBeans 6.8, Glassfish v3, JSE 6, JEE 6).

According to information found on the Web, I included in the project libraries javaee.jar and appserv-rt.jar from the Glassfish lib directory. The client connects to Glassfish from NetBeans but not outside it (class not found com.sun.enterprise.naming.serialInitContextFactory).

This class is supposed to exists in the appserv-rt.jar library. However, the latest version of this library are empty, containing just int he manifest file indirection to the libraries (.jar) located in the "lib/module" directory of the Glassfish installation. It seems that the indirection works when run from NetBeans but not outside.

In order to have it working, I added to the project libraries the two .jar files from Glassfish "lib" directory plus all the .jar files from Glassfish "lib/module" directory. And it worked !

I tried also to locate the missing classes and to add the libraries one by one, but I got some injection and linking errors I could not solve.

Maybe someone knows the minimum necessary set of libraries to add?

Hope it helps

tjquinn
Offline
Joined: 2005-03-30

Can you please post the entire command you used to start your client?

- Tim

mdynca
Offline
Joined: 2010-07-11

IN my case, I started the client with:
java -jar MyClient.jar

As the libraries were added in NetBeans (together with the .jar containing the EJB remote interface), NetBeans had put in the manifest of MyClient.jar the full classpath.

However, it doesn't work when started from another computer than the one containing the Glassfish server (I don't know why).

flery
Offline
Joined: 2010-07-13

i did the same thing. i copied all files (lib/modules) into the same directory as my jar. it solved all the problems (i can call ejb methods from any pc), but the footprint of the application is way too big. i hope that issue gets taken care of soon...

mdynca
Offline
Joined: 2010-07-11

I have done it! The secret is to use appclient (that brings the proper environment) instead of just java. It didn't use to work, but it was only a Glassfish V3.0 bug (fixed in V3.01). The steps to make it working:

1. Upgrade to Glassfish V3.01
-------------------------------------------
NetBeans 6.8 currently installs Glassfish V3.0 (with the problem). Either:
-- Use updatetool (in the "bin" of the Glassfish installation directory) to upgrade to V3.01 (it didn't work for me as I forgot to execute it as administrator on Vista)
or
-- Install NetBeans 6.9 that comes with Glassfish V3.01

I actually used a third solution: I downloaded Java EE 6 SDK (including Glassfish but without NetBeans) from http://java.sun.com/javaee/downloads/index.jsp, installed it, then declared the server in my NetBeans 6.8 as a new Glassfish v3 kind server (under a new name: "Glassfish V3.1)

2. Write and build the code
---------------------------------------
I created a new JEE application including an EJB module and a simple client module (not a Web one). In the properties of the application I manually specified the Main class of the simple client module to be ran.

Actually I had already did this before for the Glassfish V3 domain server (and it worked from NetBeans but not outside). So all I had to do was to go to the properties of the application plus each of the module to specify the new server as target. (Usually you cannot change the server afterwards, but as it is of the "Glassfish v3" kind too, you can do it.)

Then I Clean and Build everything and Deploy. I also Run from NetBeans to test.

When doing this, NetBeans creates a directory"MyAppClient" (if the project is called "MyApp") and several .jar files (refering to each other through their MANIFEST files). The miminum necessary files are:
-- MyAppClient/MyApp-app-client.jar
-- MyApp-ejb.jar

3. Run the client as a standalone client
--------------------------------------------------------
I copied the two files in a separate directory ./newdir.

I added the "bin" of the Glassfish V3.01 installation directory to the PATH (so to have the "appclient" or "appclient.bat" file in the PATH).

Then I ran the application as:
appclient -client newdir/MyApp-app-client.jar

AND IT WORKED! (With Glassfish V3.0 all I got was "NullPointer Exception").

The "appclient" tool is described here:
http://docs.sun.com/app/docs/doc/820-7701/appclient-1m?l=en&a=view&q=app...
and it brings all the necessary libraries.

Some observations
---------------------------
It seems (it obviously should, but I didn't try yet) to be possible to run appclient on a remote client machine in order to connect to a server over the network (by specifying the proper connection parameters in the proper XML files). This implies that the appclient is installed on any remote client computer. That implies that Glassfish is installed on every remote client computer?

It seems (I found on a forum - I don't remember where) that the ".jar" files must be build for each of the client platforms, as the Glassfish platform specific distribution includes same platform-native libraries in them.

The tool "appclient" allows to properly specify a user name and password in order to be able to call protected EJBs on the server.

tjquinn
Offline
Joined: 2005-03-30

I'm glad to hear it's working now. Sorry the path to success was so hard.

To use the appclient script remotely, you do not need all of GlassFish installed on the remote sytem. Unfortuately, you need quite a bit of it, something we want to improve on. The best approach is to use the package-appclient script on the server which creates a JAR file. You can then move this JAR to any remote system and expand it. It contains the correct subset of GlassFish you'll need to use the appclient script.

In earlier versions of GlassFish the JAR files did contain platform-specific content, but that has not been true for a while. So you can use the same JAR file produced by package-appclient for any operating system.

As for username and password, you can pass the username on the command line. You can also specify a password file which contains the password. (We strongly discourage including passwords on command lines.) Or, even better, the app client container and the client-side security subsystem will prompt the user automatically when it needs a username and password.

- Tim

mdynca
Offline
Joined: 2010-07-11

Hi Tim

Thank you for your comments.

Unfortunately it is still not fully working.

It works perfectly when appclient is executed on the same computer as the Glassfish server. It works even if I copy the .jar files in another directory and if I use a package-appclient generated "appclient". If I add a -targetserver option it works for the network name or IP address of the current computer as well as for localhost or 127.0.0.1 and for the right port number or no port number (for the default 3700). If I make a mistake on either the address or the port, I get an error message after a timeout period.

If I copy the .jar files on another computer it still doesn't work. No matter if I use a local Glassfish installation (same V3.01 version as on the server) or a package-appclient generated appclient, no matter if I modify the .xml file or I use the -targetserver option (or both), it doesn't work. If I make a mistake on the server address or port number, I get the error message after a while, as previously. If there is no mistake, nothing happens (the appclient is still and still running with no result - I stopped it after about ten hours - no network timeout is that long).

I tried Linux to Windows, Windows to Linux, Windows to Windows with the same results.

Of course, I disabled all firewalls (with the firewalls up and running, I get at least an error message).

Does anyone know how to do to run a remote client?

Kristian Rink

Am Fri, 29 Feb 2008 03:29:41 PST
schrieb glassfish@javadesktop.org:
[...]
> Runs still fine from Netbeans, but on the console I get:
>
> javax.naming.NoInitialContextException: Cannot instantiate class:
> com.sun.enterprise.naming.SerialInitContextFactory [Root exception is
> java.lang.ClassNotFoundException:
> 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]

How do you run it from the console? Have you been careful to have
appserv-rt.jar on your classpath while doing so?
Cheers,

Kristian

--
Kristian Rink * http://zimmer428.net * http://flickr.com/photos/z428/
jab: kawazu@jabber.ccc.de * icq: 48874445 * fon: ++49 176 2447 2771
"One dreaming alone, it will be only a dream; many dreaming together
is the beginning of a new reality." (Hundertwasser)

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: users-help@glassfish.dev.java.net

janmueller
Offline
Joined: 2008-02-20

Thats how I start it from the console:

java -classpath /home/jmueller/glassfish-v2ur1/lib/appserv-rt.jar:/home/jmueller/glassfish-v2ur1/lib/javaee.jar -jar Praktikum1-app-client.jar

Greetings,
Jan

dkoper
Offline
Joined: 2005-10-27

Wild guess, but could it be that you have the classpath wrong?

java -classpath /home/jmueller/glassfish-v2ur1/[b]glassfish[/b]/lib/appserv-rt.jar:/home/jmueller/glassfish-v2ur1/[b]glassfish[/b]/lib/javaee.jar -jar Praktikum1-app-client.jar

janmueller
Offline
Joined: 2008-02-20

Sorry, but there is no subdir "glassfish" in the directory "glassfish-v2ur1".
(I installed glassfish along with the Netbeans 6.0 JavaEE Pack)

christianr
Offline
Joined: 2008-03-06

Hi,
I'm facing a similar problem, same symptom, but I'm trying to access a JDBC pool defined in Glassfish from outside (from within an ANT build script)

I could narrow down the problem a bit:
my ANTClassLoader contained all libraries from glassfish/lib but still, I got that exception when calling new InitialContext().

I've then noticed, that the Thread.currentThread().getContextClassLoader() was not set to my ANTClassLoader. If I changed that before calling new InitialContext() I got further to the next ClassNotFoundError, which led to inclusion of also glassfish/imq/lib into my classpath.

Now it almost works, it just complains about a class from the mySQL jdbc driver which was not found ...

Christian