Execute jar file with multiple classpath libraries from command prompt
Solution 1:
You cannot use both -jar
and -cp
on the command line - see the java documentation that says that if you use -jar
:
the JAR file is the source of all user classes, and other user class path settings are ignored.
You could do something like this:
java -cp lib\*.jar;. myproject.MainClass
Notice the ;.
in the -cp
argument, to work around a Java command-line bug. Also, please note that this is the Windows version of the command. The path separator on Unix is :
.
Solution 2:
Using java 1.7, on UNIX -
java -cp myjar.jar:lib/*:. mypackage.MyClass
On Windows you need to use ';' instead of ':' -
java -cp myjar.jar;lib/*;. mypackage.MyClass
Solution 3:
Let maven generate a batch file to start your application. This is the simplest way to this.
You can use the appassembler-maven-plugin for such purposes.
Solution 4:
Regardless of the OS the below command should work:
java -cp "MyJar.jar;lib/*" com.mainClass
Always use quotes and please take attention that lib/*.jar will not work.