views:

40

answers:

3

I've built a JAR file and it executes fine on my PC (XP) which has Eclipse installed. It also works on another PC, which also has Eclipse.

I've tried running it on another PC(XP) that does not have Eclipse. Though it contains the JDK and multiple JRE. The JAR file just does not execute by clicking or from the command prompt.

I am not entirely sure, but my best guess is the Environment Variables are not set properly. Here is the error I receive from the command prompt:

Exception in thread "main" java.lang.NoClassDefFoundError: ...

Any help would be appreciated.

+3  A: 

It must be a CLASSPATH issue.

The stacktrace should also say which class it failed to find. Once you have that, then find which jar has that class. Then add that jar file to your classpath or add it to the classpath env variable.

CoolBeans
Great! It worked. I didn't know you have to explicitly add a external JAR to the classpath! Thank you.
Darth_Yoda
You are welcome. Please accept an answer as a correct answer.
CoolBeans
A: 

Environment variables are not considered when invoking a jar file when clicking on it (equivalent to running javaw -jar your.jar).

I'm pretty sure that it doesn't work on your first PC outside of Eclipse either.

Thorbjørn Ravn Andersen
+2  A: 

This is likely a classpath issue as others have said.

One thing to note is how your jar is constructed. You have a number of options in the dialog for exporting a runnable jar;

  • Extract classes into jar
  • Zip dependencies into the jar - creates jar-in-jar-loader.jar inside the jar.
  • Place jars in a subdirectory next to the jar.

Depending on what you have chosen for this depends on how the jar will behave. If the classes are extracted, dependent classes not in the JDK should be on the classpath. I'd recommend this course of action as it is simpler.

Now, the question is - are you using a dependency on your classpath not in the build dependencies of the eclipse project? If so, it won't be packed with / zipped into / put next to the jar because eclipse doesn't know about it (but java will still find it on your system because it's on the classpath). Also, if you've saved an ANT script and updated the build path in eclipse, eclipse won't update that ANT script - that is generated once only.

Ninefingers