On Linux, how to execute a Java jar file with external jar files?

On Linux, how to execute a Java jar file with external jar files?

+8
java linux jar execution
source share
2 answers

Use the -cp flag:

 java -cp /path/to/somefolder/*.jar:/path/to/otherfolder/*.jar com.YourMainClass 

Or add a Class-Path: header to the jarar manifest (see Jigar answer)


Note for those who answered using java -jar <etc> : the -jar flag disables the standard -cp flag and the CLASSPATH environment variable, because it extracts the class path from the JAR manifest. Any answer that combines -jar and -cp or $CLASSPATH will not work.

This information is well hidden, but I finally found the link:

-jar
Run the program encapsulated in the JAR file. The first argument is the JAR file name instead of the launch name. For this to work, the manifest JAR file must contain the line form Main-Class: classname. Here, classname defines a class that has the method public static void main(String[] args) , which serves as your starting point for the application. See the Jar and Jar tool reference page for a Java tutorial for information on working with Jar files and manifest Jar files. When you use this parameter, the JAR file is the source of all user classes and other parameters of the user class path are ignored.

Source: java - Java Application Launcher

+11
source share
 java -jar /path/to/externalJarFile.jar 

Update

You can add the desired library to the manifest with the heading Class-Path:

For example:

 Class-Path: MyUtils.jar 

Cm

+7
source share

All Articles