views:

105

answers:

2

I am having problems having my Java programs see classes that are packaged in an external jar. I am running under Windows 7. I have the classes embedded in a jar called ParserUtilities.jar. I established the path with a CLASSPATH variable using the System utility and the Environment tab. I confirmed that the CLASSPATH is set correctly. When I type echo %CLASSPATH%, I see C:\Program Files\Java\externaljars\ParserUtilities.jar which is correct. But when I type java -jar Parse.jar (my executable) I get the error Exception in thread "main" java.lang.NoClassDefFoundError: com/artificialmed/Initialize

Some additional information:

  1. When I put the ParserUtilities.jar in the ..\lib\ext directory, everything works.
  2. I am running java version 1.6.0_16. Java(TM) SE Runtime Environment
  3. In experimenting, I typed java -cp C:\Program Files\Java\jre6\lib\ext>java -cp C:\Program Files\Java\externaljars\ParserUtilities.jar

and got this error "Could not find the main class: Files\Java\externaljars\ParserUtilities.jar" but there is no main class (its just a collection of classes I use in a bunch of programs).

I do not have a Java SDK loaded in the environment, just a java JRE (Is this the issue?).

+1  A: 

Try adding "" around the classpath entry since you have a space in the directory (Program Files). The error message "Files\Java\externaljars\ParserUtilities.jar" would indicate that is the problem.

TofuBeer
putting double quotes arround the path when executing java -cp "C:Program Files\Java\externaljars\ParserUtilities.jar" causes the following to happen:Usage: java [-options] class [args....]etc.
Elliott
Add the double quotes on CLASSPATH variable of the windows environment.
Yannick L.
@Elliott: That's because you need to specify your main class at the end.
Bart van Heukelom
A: 

From Sun's documentation on the -jar option to the java command: "When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored." I believe that means that both your CLASSPATH environment variable as well as any -cp arguments that you might provide on the command line are both going to be ignored.

So, you have the following options:

  1. Package everything into one jar, and then you can execute it by running java -jar JarWithEverything.jar.
  2. Keep things in separate jars and provide both jars as arguments on the command line, so that you type something like this:

    java -cp "C:\Program Files\Java\externaljars\ParserUtilities.jar";"C:\Program Files\Java\externaljars\Parser.jar" com.artificialmed.Initialize

  3. Keep things in separate jars and put one or both jars in your CLASSPATH environment variable instead of providing them on the command line.

Joe Carnahan
OK. This pointed me in the right direction. I was executing my program by typing java -jar Parse.jar from the command line. This doesn't work as described above. What did work was doing suggestion 3 and then executing the program by typing java Parser (where Parser is the main class). In other words, if using the CLASSPATH variable, you can't invoke the main executable with the java -jar option. You need to just use java <program name>. Thanks for our help.
Elliott