Run a Jar File from the Command Line and Specify Classpath

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

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.

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.

Run jar with dependencies from the command line

When you use java -jar, dependencies are not specified on the command line.
You have 2 ways to add jars to the class path:

  1. Call java with the main class and add jar files, including your foobar.jar, on the command line:

    java -cp foobar.jar:baz.jar com.mycompany.MainClass
  2. Include dependencies in foobar.jar's manifest file (and then run java -jar)

    Class-Path: baz.jar

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

Run jar with classpath parameters

First, if you only plan to access files that are within the same jar as your MyMainclass, you can call getResource() or getResourceAsStream() on its class loader rather than getSystemResource() / getSystemResourceAsStream().

public class MyMainClass {

public static void main(String[] args) throws Exception {
InputStream inputStream = MyMainClass.class.getResourceAsStream(args[0]);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
}
}

Second, you should pass the path to the file rather than passing the resources folder on the classpath:

java -cp externalJar.jar;myJar.jar com.my.package.MyMainClass "/resources/myTextFileInUtf8.txt"

Please also note that it is (almost) always a good idea to specify the encoding when creating an InputStreamReader, otherwise you rely on the JVM default encoding.

How to run a class from Jar which is not the Main-Class in its Manifest file

You can create your jar without Main-Class in its Manifest file. Then :

java -cp MyJar.jar com.mycomp.myproj.dir2.MainClass2 /home/myhome/datasource.properties /home/myhome/input.txt


Related Topics



Leave a reply



Submit