Luckily or unluckily, I haven’t had to work too extensively with invoking java from the commandline up until this point, I’ve usually been using something like Maven, Ant, or running things within a servlet container. I’ve just compiled my application in Maven into one JAR using the assembly:single goal, and am having serious problems running it from the commandline.
Here’s what I’m attempting to do:
export JAVA_CLASSPATH="`pwd`:/path/to/remote/libs/"
java -cp "${JAVA_CLASSPATH}" -jar groupId-artifactId-version-jar-with-dependencies.jar com.my.main.Class
This is failing with the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: com/remote/lib/IServer
Caused by: java.lang.ClassNotFoundException: com.remote.lib.IServer
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: com.my.main.Class. Program will exit.
The IServer class is in the /path/to/remote/libs directory and isn’t being found. It also seems to not be able to find the main class, which is really odd. What am I doing wrong?
@Kal’s answer is correct, but an easier way to fix it would be this:
No need to tinker with the manifest file; just add the jar you want to the classpath.
Also, if the IServer stuff is in a jar inside
/path/to/remove/libs/then you need to explicitly include the name of the jar file. I’ve included it above and assumed it is called “iserver.jar”If you have many dependencies then it’s often easier to write a shell script for launching your code. The script can build a (potentially very large) classpath string using shell glob goodness. The java executable doesn’t do any globbing.
EDIT: Note that I’ve assumed that you don’t have access to Ant or Maven or any of that good stuff, since you stated that you usually use those tools but are not doing it right now.