Execute jar file with multiple classpath libraries from command prompt
Solution 1
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 2
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 3
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 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.
Solution 5
You can use maven-assembly-plugin, Here is the example from the official site: https://maven.apache.org/plugins/maven-assembly-plugin/usage.html
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>your main class</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
Praneeth
Updated on May 19, 2020Comments
-
Praneeth about 4 years
I have a Maven project which generates a jar file and copies all dependencies to
target/lib
folder. I want to execute this project on client's machine (windows). So, I copiedmyproject.jar
toC:\xyz
folder and all dependencies toC:\xyz\lib
folder. How do I execute this project from client's command prompt? I tried to usejava -cp lib\*.jar -jar myproject.jar
fromC:\xyz
folder but it throws following error.Exception in thread "main" java.lang.NoClassDefFoundError: lib\commons-codec-1/3/jar Caused by: java.lang.ClassNotFoundException: lib\commons-codec-1.3.jar at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) Could not find the main class: lib\commons-codec-1.3.jar. Program will exit.
I think if I specify all dependencies in classpath (like
java -cp lib\dep1.jar;dep2.jar
), it will get rid of the problem but I don't want to do this as I have 40 libraries already and it might grow in future releases. Is there a better way to do this?