user2413527 user2413527 - 6 months ago 25
Java Question

unix java classpath cp adding

This seemingly simple problem is stumping me. I have a jar with a set of dependencies which I cannot seem to get working in linux. All jars and the script reside in the same directory.

This one picks up and tries to run my main class

java -cp MyJar.jar com.mylib.MyMainClass


but of course it throws a
ClassNotFoundException
on one of the dependency classes. So then I try to add one of the dependencies like so

java -cp MyJar.jar:MyDependencyJar.jar com.mylib.MyMainClass


Then it says
ClassNotFoundException
for
com.mylib.MyMainClass
!

I also tried these with various same problems

java -cp . com.mylib.MyMainClass


and this

java -cp *.jar com.mylib.MyMainClass


and this

java -cp .:*.jar com.mylib.MyMainClass


Any idea please ?

Answer

Try this one:

java -cp /path/to/jar1.jar:/path/to/jar2.jar:. com.mylib.MyMainClass

Note that you should not omit that final . in the classpath, which represent the current working directory, because using -cp would override your previous classpath setting.