1

I'm working my way through a book about "Java EE 7 for Glassfish", with the server installed on Fedora Linux.

I have a simple stateless session bean SimpleSessionBean deployed on the server and I am trying to approach that SimpleSessionBean via SessionBeanClient and the Glassfish command line tool appclient, running a client jar. Everything from the book, so it should work. The client however can't find SimpleSessionBean. Apparently a class path issue. In the server logs nothing happened.

I can't find any pointers how Glassfish should be properly installed. Everything works within the server. I can approach installed war files from facelets running in a browser.

It is probably a matter of setting $PATH right or something or some other environment variable. Any pointers to relevant literature?

Thanks in advance for any suggestions!

UPDATE1: error message

From the bash terminal window where I run appclient:

[fedora@localhost bin]$ ./appclient -client /home/fedora/Downloads/6886EN_04_Code/ch04_src/simplesessionbeanclient/target/simplesessionbeanclient.jar

Jul 06, 2017 12:52:57 PM org.glassfish.apf.impl.DefaultErrorHandler error SEVERE: Class [ Lnet/ensode/glassfishbook/SimpleSession; ] not found.

Error while loading [ class net.ensode.glassfishbook.SessionBeanClient ] Exception in thread "main" java.lang.NoClassDefFoundError: net/ensode/glassfishbook/SimpleSession at net.ensode.glassfishbook.SessionBeanClient.invokeSessionBeanMethods(SessionBeanClient.java:12) at net.ensode.glassfishbook.SessionBeanClient.main(SessionBeanClient.java:19) Caused by: java.lang.ClassNotFoundException: net.ensode.glassfishbook.SimpleSession at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at org.glassfish.appclient.client.acc.ACCClassLoader.findClass(ACCClassLoader.java:237) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

UPDATE2: From the Glassfish book:

We are using Maven to build our code. For this example, we used the Maven Assembly plugin (http://maven.apache.org/plugins/maven-assembly-plugin/) to build a client JAR file that includes all dependencies; this frees us from having to specify all the dependent JAR files in the -classpath command-line option of the appclient utility. To build this JAR file, simply invoke mvn assembly:assembly from the command line.

SOLUTION: the missing link was producing a client jar with additional jar's "on board" so to speak. Proceed as follows (at least in Eclipse): select pom.xml > right-click > Run As > Maven build... > enter in Goals field: assembly:assembly> Apply/Run.

The result will be that you will find TWO jars under your target folder: xxxclient.jar and xxxclient-jar-with-dependencies.jar. From the command line in bash execute from the folder with latter jar:

/path_to/appclient -client xxxclient-jar-with-dependencies.jar

After a very long wait (on my $200 mini Linux box) the HelloWorld-ish server EJB gets finally properly called.

jhulst
  • 353
  • 1
  • 3
  • 18

2 Answers2

2

Your assumption is right.

You are missing net.ensode.glassfishbook.SimpleSession in your classpath.

From an older book online:

...executed through the appclient utility. This utility can be found at [glassfish installation directory]/glassfish/bin/. Assuming this path is in the PATH environment variable, and assuming we placed our client code in a JAR file called simplesessionbeanclient.jar, we would execute the above client code by typing the following command in the command line:

appclient -client simplesessionbeanclient.jar

It seems that you've started from

.../bin/./appclient -client /home/fedora/Downloads/6886EN_04_Code/ch04_src/simplesessionbeanclient/target/simplesessionbeanclient.jar

You need SimpleSession.class in your CLASSPATH (or in a jar in that classpath). Usually java checks the current directory first (which is your bin folder). If the class is not found (its not, since its in your simplesessionbeanclient folder), it searches for that class in the classpath (where you did not add the simplesessionbeanclient folder).

Try

appclient -client simplesessionbeanclient.jar

from the folder where simplesessionbeanclient.jar is located. If you don't want to add the appclient folder to your path start with

/your/path/to/appclient -client simplesessionbeanclient.jar

(again from the folder where simplesessionbeanclient.jar is located)

Update: If you still get a ClassNotFoundException have a look if it is missing in your jar file (jars are Zip-File, you could use your Zip-Tools):

jar tf simplesessionbeanclient.jar

if there is a SimpleSession.class

Arigion
  • 3,267
  • 31
  • 41
  • The book says what you are suggesting. But if I run that statement "./appclient -client simpl*.jar", the error msg is: "unable to access jarfile simplesessonbeanclient.jar". That path "6886EN..." btw is a path to an Eclipse workspace, from where I did Maven build the jar before I deployed it to Glassfish manually via the Glassfish console. Obivously I need to work with the client.jar as it is deployed in the server, not the Eclipse workspace. – jhulst Jul 06 '17 at 12:28
  • I added an UPDATE2 to my original post. Perhaps the key to the problem is there. Adding individual jar's to the classpath can't be the proper solution. See no option mvn assemby:assembly from within Eclipse. Need to figure out how to produce a jar this way. – jhulst Jul 06 '17 at 12:32
  • "Obivously I need to work with the client.jar as it is deployed in the server" <- That does not sound right.You deploy your SimpleSessionBean to the server, not the client jar. – Arigion Jul 06 '17 at 12:34
  • Of course, you are right. Perhaps this is a useful link: https://stackoverflow.com/questions/15461053/application-client-using-ejb-annotation-and-maven-on-glassfish – jhulst Jul 06 '17 at 12:37
  • "./appclient -client simpl*.jar" <- are you sure you where in the "/home/fedora/Downloads/6886EN_04_Code/ch04_src/simplesessionbeanclient/target/" folder when you issued that command? – Arigion Jul 06 '17 at 12:38
  • I wasn't. I was in the appclient folder. When I execute the client as you suggest, I get exactly the same error message as above. This is probably the crucial information I finally found: http://docs.oracle.com/cd/E18930_01/html/821-2418/beakv.html – jhulst Jul 06 '17 at 12:49
  • For your Update2 question: Eclipse -> Right click on Project -> Run as -> Maven build... -> under goals "assemby:assembly" – Arigion Jul 06 '17 at 12:54
  • It works! Will update my original post with details. Thanks to Arigion + kudos. – jhulst Jul 06 '17 at 13:43
0

I did the following to fix my problem:

  • Use appclient -classpath (instead of appclient -client)
  • Use the regular project JARs (instead of the one generated by mvn assembly:assembly)
  • Deploy the EJB to Glassfish (simplesessionbean.jar)

The example code from a more recent book "Java EE 8 Application Development" by David R. Heffelfinger (the same author of "Java EE 7 for Glassfish") is almost exactly the same (the only minor difference is classes are packaged in "net.ensode.javaee8book" instead of "net.ensode.glassfishbook").

When running appclient.bat -client simplesessionbeanclient-jar-with-dependencies.jar I kept getting:

java.lang.ClassNotFoundException: <mainclass>

errors. This was because the POM was assembling a manifest with <mainClass> value of "net.ensode.glassfishbook.SessionBeanClient" (instead of "net.ensode.javaee8book.SessionBeanClient"). So I decided to avoid using the -client option for appclient.bat and switched to -classpath which allowed me to specify the main class on the command line (which is easier than updating the POM or refactoring the packages to suit the manifest).

But then when running the appclient command:

PS C:\home\programs\java_ee_sdk-8u1\glassfish5\glassfish\bin> .\appclient.bat -classpath "C:\home\code\Java-EE-8-Application-Development-Code-Samples-master\ch04_src\simplesessionbean\target\simplesessionbean.jar;C:\home\code\Java-EE-8-Application-Development-Code-Samples-master\ch04_src\simplesessionbeanclient\target\simplesessionbeanclient.jar" net.ensode.javaee8book.SessionBeanClient

I kept getting:

Root exception is javax.naming.NameNotFoundException: net.ensode.javaee8book.SimpleSession#net.ensode.javaee8book.SimpleSession not found]] 

errors. This was solved by deploying the EJB (simplesessionbean.jar) to Glassfish via the Admin Console (this missing step was not mentioned in the book). Running the appclient.bat command then worked.

Screenshot of appclient.bat (takes about 15 seconds to load):

enter image description here

Screenshot of EJB deployment:

enter image description here

Alternatively

You can manually compile the client to include all the dependencies by copying SimpleSession.java and SimpleSessionBean.java from the "simplesessionbean" project to the "simplesessionbeanclient" project (remember to refactor the package statements). This will generate simplesessionbeanclient.jar with the EJBs included (Nb: you still have to deploy the EJBs to the GlassFish server). Also make sure that the <mainClass> element in the POM points to the correct package.

enter image description here

You can now use the -client option:

enter image description here

mvanle
  • 1,847
  • 23
  • 19