aspose file tools*
The moose likes Beginning Java and the fly likes Help with sample JMS program. Getting NoClassDefFoundError, ClassNotFoundException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with sample JMS program. Getting NoClassDefFoundError, ClassNotFoundException" Watch "Help with sample JMS program. Getting NoClassDefFoundError, ClassNotFoundException" New topic
Author

Help with sample JMS program. Getting NoClassDefFoundError, ClassNotFoundException

Philip Jenson
Greenhorn

Joined: Jan 24, 2007
Posts: 21
Greetings,

I am trying to work through a sample JMS application from the O'Reilly book Java Message Service (2nd edition). The code from the book is posted at the bottom of this post.

The sample Chat program is very simple. The book mentions that I need to add activemq-all-5.2.0.jar to my classpath. So I used the following command to build the program:

javac -cp "C:\Program Files\Apache Software Foundation\apache-activemq-5.2.0\activemq-all-5.2.0.jar" Chat.java

This works. The class file gets created successfully. However, when I try to run the program, I get an exception. The command I am using to run it is this:

java Chat TopicCF topic1 Fred

Exception in thread "main" java.lang.NoClassDefFoundError: Chat
Caused by: java.lang.ClassNotFoundException: Chat
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: Chat. Program will exit.

I've literally tried everything that I can think of. I thought maybe it was happening because of the package declaration but I don't think that's it. I've confirmed the directory structure is correct and even removed the package declaration altogether. I've searched endlessly for answers online but I can't seem to get past this.

If anyone could take a look at this, I would appreciate it greatly. I want to continue reading through this book but I've hit a roadblock that I can't seem to get past. Thanks!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39388
    
  28
Try adding . to your CLASSPATH. Do you have a system CLASSPATH set?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You'll need to run it with ActiveMQ on the classpath as well.
Philip Jenson
Greenhorn

Joined: Jan 24, 2007
Posts: 21
David, I ran the following command in an effort to do what you said:
java -cp "C:\Program Files\Apache Software Foundation\apache-activemq-5.2.0\activemq-all-5.2.0.jar" Chat TopicCF topic1 Fred

Is that what you meant? In any case, I get the same exception:

Exception in thread "main" java.lang.NoClassDefFoundError: Chat
Caused by: java.lang.ClassNotFoundException: Chat
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: Chat. Program will exit.


Campbell, are you suggesting I add . to my environment variable CLASSPATH or to the javac/java commands? I do have a system CLASSPATH variable but the only thing in it was a specific entry for an unrelated application. I changed it so that it contains the period and nothing else. Before ever creating this thread, I set the ActiveMQ jar in my system CLASSPATH. I had the exact problems I'm having now, except then I didn't have to add the -cp arguments.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

If you provide a "-cp" argument it must contain the entire classpath. In general I avoid setting the CLASSPATH environment variable, rather I create batch/shell scripts that either build a classpath from files, or just have it hard-coded. The same batch/sheel script also executes the java command.
Philip Jenson
Greenhorn

Joined: Jan 24, 2007
Posts: 21
I'm assuming I don't have to add the . to the -cp argument, right? I'm grasping for straws, so I tried that, even though it seems redundant since I added the . to the the system CLASSPATH. Or does the -cp argument override the system-set CLASSPATH? Either way, I tried adding the . to the -cp argument.

java -cp ".;C:\Program Files\Apache Software Foundation\apache-activemq-5.2.0\activemq-all-5.2.0.jar" Chat TopicCF topic1 Fred

Gives me this error:

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
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at Chat.<init>(Chat.java:19)
at Chat.main(Chat.java:81)

At this point, I don't care what I need to do to get this running. The book just says "simply include the activemq-all-5.2.0.jar file in your classpath). It makes it seem so simple, so I feel like I have to be missing something. Based on what I've provided in this thread, is there an obvious reason that I'm having these problems?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

"-cp" must provide all the classpath (sorry, I thought I said that).

How are you handling the JNDI stuff?
Philip Jenson
Greenhorn

Joined: Jan 24, 2007
Posts: 21
Well, I have a jndi.properties file in the same location as the source code. Based on the errors I'm getting, it seems to me that it's not even getting far enough to read that file, though. Is that correct or could this potentially be part of the problem?
Philip Jenson
Greenhorn

Joined: Jan 24, 2007
Posts: 21
I just got it to work! I'm very happy that it's working, but I'm hoping someone can clarify why it works now so I can learn something out of this.

I used this argument on the javac/java commands:
-cp "<source files location>;C:\Program Files\Apache Software Foundation\apache-activemq-5.2.0\activemq-all-5.2.0.jar"

I don't understand why that worked but putting the . there did not work. In the future, what would be the best way to do something like this? Let's assume my CLASSPATH environment variable is set to . and nothing else. I want to compile and run a program that depends on the activemq-all-5.2.0.jar file. Using the -cp argument seems like it adds confusion, so is there an easier way? I'm thinking I should be able have java use the environment variable CLASSPATH in addition to something I specify.

What do you guys think? Is there a different approach that's all-around easier?
Philip Jenson
Greenhorn

Joined: Jan 24, 2007
Posts: 21
My current method seems to work best, but I'm still very interested to hear what you guys have to say.

What I did was move the activemq-all-5.2.0.jar file into the jdk1.6.0_21\jre\lib\ext directory.

Doing that and having the JNDI file in the current directory (with my system CLASSPATH variable set to .) works.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

If you set "-cp" it must include *all* classpath entries (sorry, I thought I said that). Using the CLASSPATH environment variable just causes confusion, particularly if it's already set up for other environments/apps/etc. Please consider doing what I suggested earlier.

The classpath must include all dependent libraries as well as your compiled classes. If you're using relative directories you must be in the correct directory when running. That's all there is to it.
Chip McCormick
Greenhorn

Joined: Oct 14, 2010
Posts: 1
I just got the same example working with the following command (use ; as separator for Windows):
java -cp activemq-all-5.4.1.jar:. Chat TopicCF topic1 Fred

That jar was copied to the same directory as the class file
---
Details specific to ActiveMQ:

ActiveMQ will need to be running (..apache-activemq-5.4.1/bin/macosx ./activemq start) and you'll need a jndi.properties file in that same directory that looks like

java.naming.factory.initial = org.apache.activemq.jndi.ActiveMQInitialContextFactory
java.naming.provider.url = tcp://localhost:61616
#the URL for Mac is something more like tcp://My-MacBook.local:61616
java.naming.security.principal=system
java.naming.security.credentials=manager

connectionFactoryNames = TopicCF
topic.topic1 = jms.topic1
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39388
    
  28
Paulo Cassiano,
Your post was moved to a new topic.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with sample JMS program. Getting NoClassDefFoundError, ClassNotFoundException