Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jar not found when executing class

I'm working through a ANTLR (a language processing library) 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 library 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.

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?

like image 589
Simon Avatar asked Feb 27 '23 22:02

Simon


1 Answers

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?

-cp on the commandline overrides the CLASSPATH variable. There is no convenient way to do what you're trying to do. I'd suggest creating an ant script, shell script, or shell alias if you don't want to type out the full classpath each time.

like image 67
Brett Kail Avatar answered Mar 05 '23 18:03

Brett Kail