"java -jar" vs "java -cp" question

M

Markus Dehmann

I have my classes with the main method in main.jar, and an external
library in external.jar.

Now, I can start my application with:
java -cp main.jar:external.jar MainClass

Fine, but the following command does *not* work:
java -cp external.jar -jar main.jar MainClass

It doesn't find the classes in external.jar. Why not??

Thanks!
Markus
 
H

hinduu

Hi Markus

Here is the statement about -jar option in the java docs

-jar Executes a program encapsulated in a JAR archive. The
first argument is the name of a JAR file instead of a
startup class name.
In order for this option to work, the manifest of the
JAR file must contain a line of the form Main-
Class:classname. Here, classname identifies the class
having the public static void main(String[] args)
method that serves as your application's starting
point. See the Jar tool reference page and the Jar
trail of the Java Tutorial for information about work-
ing with Jar files and Jar-file manifests. When you
use this option, the JAR file is the source of all
user classes, and other user class path settings are
ignored.

The last sentence explains your scenario

-H
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top