Execute Jar File with Multiple Classpath Libraries from Command Prompt

Run a JAR file from the command line and specify classpath

When you specify -jar then the -cp parameter will be ignored.

From the documentation:

When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.

You also cannot "include" needed jar files into another jar file (you would need to extract their contents and put the .class files into your jar file)

You have two options:

  1. include all jar files from the lib directory into the manifest (you can use relative paths there)
  2. Specify everything (including your jar) on the commandline using -cp:

    java -cp MyJar.jar:lib/* com.somepackage.subpackage.Main

Execute jar file with multiple classpath libraries from command prompt

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.

Adding multiple jars to classpath on commandline

Remove the spaces from the classpath and add the current path

javac -cp jar/A.jar:jar/B.jar:jar/C.jar:jar/D.jar:. MyFile.java

Since Java 6 you can use classpath wilcards

javac -cp jar/*:. MyFile.java

Call java -jar MyFile.jar with additional classpath option

You use either -jar or -cp, you can't combine the two. If you want to put additional JARs on the classpath then you should either put them in the main JAR's manifest and then use java -jar or you put the full classpath (including the main JAR and its dependencies) in -cp and name the main class explicitly on the command line

java -cp 'MyProgram.jar:libs/*' main.Main

(I'm using the dir/* syntax that tells the java command to add all .jar files from a particular directory to the classpath. Note that the * must be protected from expansion by the shell, which is why I've used single quotes.)

You mention that you're using Ant so for the alternative manifest approach, you can use ant's <manifestclasspath> task after copying the dependencies but before building the JAR.

<manifestclasspath property="myprogram.manifest.classpath" jarfile="MyProgram.jar">
<classpath>
<fileset dir="libs" includes="*.jar" />
</classpath>
</manifestclasspath>

<jar destfile="MyProgram.jar" basedir="classes">
<manifest>
<attribute name="Main-Class" value="main.Main" />
<attribute name="Class-Path" value="${myprogram.manifest.classpath}" />
</manifest>
</jar>

With this in place, java -jar MyProgram.jar will work correctly, and will include all the libs JAR files on the classpath as well.

set java classpath to multiple folders that have jar files and run from command line

Here is the solution that finally worked as expected. When the '*' character is used in the classpath for my specific scenario, it skipped everything after the first path. Using the double quotes(") for each path separately and then using ";" as the delimiter is the solution.

Having any space before or after the semi-colon ";" will also not work

javac -verbose -classpath "C:\Program Files\lib\java\core\*";"C:\Program Files\lib\java\core\locale\*";"C:\Program Files\lib\java\modules\*";"C:\Program Files\lib\java\modules\ext\*" testClass.java



Related Topics



Leave a reply



Submit