Jar not found when executing class

Posted by Simon on Stack Overflow See other posts from Stack Overflow or by Simon
Published on 2010-05-28T17:59:36Z Indexed on 2010/05/28 18:02 UTC
Read the original article Hit count: 645

Hi there,

I'm working through the ANTLR book and there are many examples that should be easy to compile using the command line.

Some information to get te problem:

antlr-3.2.jar contains the ANTLR classes. I added the antlr-3.2.jar to the CLASSPATH environment variable (Windows 7) and when compiling the classes with javac everything works fine. This is what i execute to compile my program:

javac Test.java ExprLexer.java ExprParser.java

Test.java contains my main()-method whereas ExprLexer and ExprParser are generated by ANTLR. All three classes use classes contained in the antlr-3.2.jar. But so far so good. As I just said, compiling works fine.

It's when I try to execute the Test.class that I get trouble. This is what I type:

java -cp ./ Test

When executing this, the interpreter tells me that he can't find the ANTLR-classes contained in the antlr-3.2.jar, altough I added an entry in the CLASSPATH variable.

E:\simone\Programmierung\Language Processing Tools\ANTLR\Book Samples and Exercises\Exercise\1\output\Test.java

Exception in thread "main" java.lang.NoClassDefFoundError: org/antlr/runtime/Cha
rStream
Caused by: java.lang.ClassNotFoundException: org.antlr.runtime.CharStream
        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: Test.  Program will exit.

I'm using Windows 7 and Java 1.6_20. Can someone tell what is going on? Why will the interpreter not look in the jar-Archive I specified in the CLASSPATH?

I found some kind of workaroud. I copied the antlr-3.2.jar into the directory where the Test.class is located and then executed:

java -cp ./;antlr-3.2.jar Test

This worked out. But I don't want to type the jar-Archive everytime I execute my test programs. Is there a possibility to tell the interpreter that he should automatically look into the archive?

© Stack Overflow or respective owner

Related posts about java

Related posts about command-line