• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help with sample JMS program. Getting NoClassDefFoundError, ClassNotFoundException

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try adding . to your CLASSPATH. Do you have a system CLASSPATH set?
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'll need to run it with ActiveMQ on the classpath as well.
 
Philip Jenson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"-cp" must provide all the classpath (sorry, I thought I said that).

How are you handling the JNDI stuff?
 
Philip Jenson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paulo Cassiano,
Your post was moved to a new topic.
 
reply
    Bookmark Topic Watch Topic
  • New Topic