Benjamin
Benjamin

Reputation: 541

How do I add a .jar file to the classpath on Mac OS X?

I'm starting to become pretty irritated, because EVERY time I try to add a JAR file to the classpath to run a program (which compiles fine in Eclipse) I get a "java.lang.NoClassDefFoundError" exception. The file swt.jar and GUI.class (main class) are in the same directory. If I issue this command: "java -cp swt.jar GUI", I get this error message:

Exception in thread "main" java.lang.NoClassDefFoundError: GUI Caused by: java.lang.ClassNotFoundException: GUI 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:306) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

If I formulate the command like this: "java GUI -cp swt.jar", it'll throw this exception:

Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/swt/widgets/Display at GUI.main(GUI.java:9) Caused by: java.lang.ClassNotFoundException: org.eclipse.swt.widgets.Display 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:306) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:247) ... 1 more

I just want to know how I can add .jar files to the runtime classpath on mac.
All of these commands are being issued from the Terminal on a 64-bit MacBook running Mac OS X Lion.

Upvotes: 2

Views: 4769

Answers (2)

James
James

Reputation: 2542

The problem, I think, is that you are specifying swt.jar as the classpath, but the file you're using is not in the jar. You neeed to specify both the jar and this dir as the classpath.

e.g.

java -cp swt.jar:./ GUI

Upvotes: 2

duffymo
duffymo

Reputation: 308733

This one will work, with modifications:

If I formulate the command like this: "java GUI -cp swt.jar"

You just didn't go far enough. You have to add ALL the 3rd party JARs, including the ones for Eclipse SWT and all your other dependencies.

Upvotes: 0

Related Questions