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

Discussion in 'Java' started by Markus Dehmann, Sep 28, 2005.

  1. 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
    Markus Dehmann, Sep 28, 2005
    #1
    1. Advertising

  2. Markus Dehmann

    Guest

    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


    Markus Dehmann wrote:
    > 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
    , Sep 28, 2005
    #2
    1. Advertising

  3. Markus Dehmann

    Roedy Green Guest

    Roedy Green, Sep 28, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thomas Kellerer
    Replies:
    5
    Views:
    34,533
    Nomak
    Apr 13, 2005
  2. NetKev
    Replies:
    0
    Views:
    1,945
    NetKev
    May 26, 2006
  3. David

    Java jar file error

    David, Aug 13, 2006, in forum: Java
    Replies:
    1
    Views:
    383
    Roland de Ruiter
    Aug 13, 2006
  4. Scott Harper

    Using java -jar

    Scott Harper, Oct 5, 2006, in forum: Java
    Replies:
    2
    Views:
    321
    Simon Brooke
    Oct 6, 2006
  5. Teber

    Java JAR files

    Teber, Feb 21, 2008, in forum: Java
    Replies:
    12
    Views:
    853
    Teber
    Feb 22, 2008
Loading...

Share This Page