153

I've compiled a JAR file and specified the Main-Class in the manifest (I used the Eclipse Export function). My dependencies are all in a directory labeled lib. I can't seem to get a straight answer on how to execute my JAR file while specifying it should use the lib/* as the classpath.

I've tried:

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

etc...

Each gives an error saying:

Error: Could not find or load main class ....

or gives the NoClassDefFoundError indicating the libraries are not being found.

I even tried remaking the JAR file and included the lib directory and contents, but still no dice...

How can I execute a JAR file from the command line and specify the classpath to use?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
SnakeDoc
  • 13,611
  • 17
  • 65
  • 97
  • Possible duplicate of [Execute jar file with multiple classpath libraries from command prompt](http://stackoverflow.com/questions/13018100/execute-jar-file-with-multiple-classpath-libraries-from-command-prompt) – Adam Michalik Feb 17 '16 at 10:50

5 Answers5

261

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
  • 27
    ; instead of : if you use windows as follows java -cp MyJar.jar;lib/* com.somepackage.subpackage.Main – Error Sep 06 '16 at 04:11
  • _You also cannot "include" needed jar files into another jar file._ Sure you can. If it's for internal use (ie, you expect the user to have some technical skill etc.) you can just ask that step 1, before attempting to run the jar, is to run `unzip MyJar.jar lib/*.jar`. Then, as you suggest, they can run `java -cp MyJar.jar:lib/* ...` – Roger Oct 31 '16 at 21:20
  • 4
    the bummer is that its a common use case to run an "all inclusive" uber jar with a reference to an external server-specific properties file. Obviously not a blocker, but certainly a less verbose command if you could use the -jar and not have to specify the main – Andrew Norman Jan 19 '18 at 20:09
  • 1
    Took me a while to figure out. And takes about 6 lines to load a log4j file explicitly. Another Java bug that will never get fixed. – Tuntable May 28 '19 at 02:37
  • 3
    Note that the manifesst Class-Path can only be used for loading jar files. It cannot be used to lookup log4j3.xml, for example. Got me good. – Tuntable May 28 '19 at 03:33
49

Run a jar file and specify a class path like this:

java -cp <jar_name.jar:libs/*> com.test.App

jar_name.jar is the full name of the JAR you want to execute

libs/* is a path to your dependency JARs

com.test.App is the fully qualified name of the class from the JAR that has the main(String[]) method

The jar and dependent jar should have execute permissions.

Sid
  • 693
  • 6
  • 8
16

You can do these in unix shell:

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

You can do these in windows powershell:

java -cp "MyJar.jar;lib\*" com.somepackage.subpackage.Main
tars
  • 161
  • 1
  • 3
6

Alternatively, use the manifest to specify the class-path and main-class if you like, so then you don't need to use -cp or specify the main class. In your case it would contain lines like this:

Main-Class: com.test.App
Class-Path: lib/one.jar lib/two.jar

Unfortunately you need to spell out each jar in the manifest (not a biggie as you only do once, and you can use a script to build the file or use a build tool like ANT or Maven or Gradle). And the reference has to be a relative or absolute directory to where you run the java -jar MyJar.jar.

Then execute it with

java -jar MyJar.jar
HankCa
  • 9,129
  • 8
  • 62
  • 83
  • 1
    I think the Class-Path needs to refer to files within the outer jar. I could not get this to work in general, to refer to config files outside of MyJar.jar. – Tuntable May 28 '19 at 02:39
0

You can do a Runtime.getRuntime.exec(command) to relaunch the jar including classpath with args.