java -jar ignores classpath — Workaround

When you want to run a Java class wich needs additional libraries, you usually run java -cp mylib.jar MyClass or you specify the environment variable $CLASSPATH before running the class.

When you have a JAR file you want to run, you usually do this by issuing java -jar myjarfile.jar

If your’re smart, you’ll think: Hey, I want to run a JAR file with some additional library so I issue java -cp mylib.jar -jar myjarfile.jar ! Forget about it, it won’t work. Believe me. Look into the java man page (section “-jar”) if you don’t believe me. I wasted hours on sorting that out. Even setting the $CLASSPATH variable won’t work. Here’s the solution:

  1. Open the jar file with some ZIP program (JAR = renamed ZIP).
  2. Open META-INF/MANIFEST.MF in a text editor.
  3. Copy the path and name of the main class: If you see a line like “Main-Class: some.package.ClassName”, it’s the part after the colon.
  4. Run this on your command line instead of the command that doesn’t work: java -cp mylib.jar:myjarfile.jar some.package.ClassName

Now you shouldn’t get any more NoClassDefFoundErrors…

Concrete example: If you want to run Lecturnity Player under Linux, just download the .jar, install the Java Media Framework (I used the “Cross-platform Java” version) and run the following command:

java -cp /opt/JMF/lib/jmf.jar:player_30.jar imc.epresenter.player.Manager

This assumes that you put the Java Media Framework to /opt/JMF and you are in the directory where player_30.jar resides.

2 thoughts on “java -jar ignores classpath — Workaround

Leave a Reply

Your email address will not be published. Required fields are marked *