How to create an executable jar

Discussion in 'Java' started by Frank Burns, May 13, 2004.

  1. Frank Burns

    Frank Burns Guest

    What do I have to do to create a jar that I can double-click and it will
    automatically run the class that contains the main() method and thus the
    application?
    Thanks
    Frank
     
    Frank Burns, May 13, 2004
    #1
    1. Advertising

  2. Frank Burns

    Chris Smith Guest

    Frank Burns wrote:
    > What do I have to do to create a jar that I can double-click and it will
    > automatically run the class that contains the main() method and thus the
    > application?


    You need a manifest file that specifies the class to run. There are
    options to the J2SDK's jar tool to specify one, but I find that the
    easiest way is simply to create a file called META-INF/Manifest.mf and
    zip that up along with everything else using a standard zip tool; then
    rename the result to have a .jar extension.

    The manifest file should start like this:

    Manifest-Version: 1.0
    Main-Class: com.mypackage.MyClass

    From there on, you can include any standard manifest attributes, as well
    as the executable jar "Class-Path", which provides extra JAR files in
    the same directory as your executable JAR that should be included in the
    classpath of the application.

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, May 13, 2004
    #2
    1. Advertising

  3. Frank Burns

    Mike Smith Guest

    That seems pretty confusing. You put the manifest in the jar as Chris says
    but there is no need for a "second jar" and there is no need for the
    classpath, everything is referenced relative to the one jar. That is the
    whole point in a double-clickable jar, you have one file to deploy to your
    users and everything they need is there. If you have two files then things
    can get out of sync. Why two jars Chris? Am I missing something?

    Mike


    "Chris Smith" <> wrote in message
    news:4.net...
    > Frank Burns wrote:
    > > What do I have to do to create a jar that I can double-click and it will
    > > automatically run the class that contains the main() method and thus the
    > > application?

    >
    > You need a manifest file that specifies the class to run. There are
    > options to the J2SDK's jar tool to specify one, but I find that the
    > easiest way is simply to create a file called META-INF/Manifest.mf and
    > zip that up along with everything else using a standard zip tool; then
    > rename the result to have a .jar extension.
    >
    > The manifest file should start like this:
    >
    > Manifest-Version: 1.0
    > Main-Class: com.mypackage.MyClass
    >
    > From there on, you can include any standard manifest attributes, as well
    > as the executable jar "Class-Path", which provides extra JAR files in
    > the same directory as your executable JAR that should be included in the
    > classpath of the application.
    >
    > --
    > www.designacourse.com
    > The Easiest Way to Train Anyone... Anywhere.
    >
    > Chris Smith - Lead Software Developer/Technical Trainer
    > MindIQ Corporation
     
    Mike Smith, May 14, 2004
    #3
  4. Frank Burns

    Roedy Green Guest

    Roedy Green, May 14, 2004
    #4
  5. Frank Burns

    KC Wong Guest

    > What do I have to do to create a jar that I can double-click and it will
    > automatically run the class that contains the main() method and thus the
    > application?


    Create a text file (whatever name will do, e.g. a.txt). In it, type:

    Main-Class: {class name - the one you use in command line}

    Note that you MUST have a new line after the line, and it is case-sensitive.
    Then use the JAR tool with the m option:

    jar -cfm Foo.jar a.txt {list of files to put inside jar}
     
    KC Wong, May 14, 2004
    #5
  6. Frank Burns

    Chris Smith Guest

    Mike Smith wrote:
    > That seems pretty confusing. You put the manifest in the jar as Chris says
    > but there is no need for a "second jar" and there is no need for the
    > classpath, everything is referenced relative to the one jar. That is the
    > whole point in a double-clickable jar, you have one file to deploy to your
    > users and everything they need is there. If you have two files then things
    > can get out of sync. Why two jars Chris? Am I missing something?


    There's no requirement to use the "Class-Path" manifest attribute or
    additional JARs. If you don't want to use them, you can forget about
    it.

    But let's say I distribute my application with Jakarta Commons
    HttpClient, and that in turn depends on Jakarta Commons Logging. I
    *could* unzip those jars and package the classes with my own, but that's
    far from ideal. Instead, I can place those JARs in the same directory
    as my own, and then list them in the manifest "Class-Path" attribute.
    That saves me from having to muck around with the Jakarta Commons
    libraries' JAR contents and mix them with my own.

    The advantages of an executable JAR are retained; this is separate from
    the environment-variable or command-line classpaths, which are still
    ignored. The paths of these jars are specified relative to the
    executable JAR, *not* relative to the current working directory or
    anything else that can change. So it's still position-independent and
    environment-independent, but I chose to keep my code separated from
    third-party code, so it's easier to drop-in replace the third-party code
    later.

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, May 14, 2004
    #6
  7. Frank Burns

    Liz Guest

    "KC Wong" <> wrote in message
    news:...
    > > What do I have to do to create a jar that I can double-click and it will
    > > automatically run the class that contains the main() method and thus the
    > > application?

    >
    > Create a text file (whatever name will do, e.g. a.txt). In it, type:
    >
    > Main-Class: {class name - the one you use in command line}
    >
    > Note that you MUST have a new line after the line, and it is

    case-sensitive.
    > Then use the JAR tool with the m option:
    >
    > jar -cfm Foo.jar a.txt {list of files to put inside jar}
    >


    Here is very simple thing to have:

    C:\bin>unzip -t DilbertComic.jar
    Archive: DilbertComic.jar
    testing: META-INF/ OK
    testing: META-INF/MANIFEST.MF OK
    testing: DilbertComic.class OK
    No errors detected in compressed data of DilbertComic.jar.

    C:\bin>unzip -p DilbertComic.jar META-INF/MANIFEST.MF
    Manifest-Version: 1.0
    Created-By: 1.4.1_02 (Sun Microsystems Inc.)
    Main-Class: DilbertComic
     
    Liz, May 14, 2004
    #7
  8. Frank Burns

    KC Wong Guest

    > Here is very simple thing to have:
    > C:\bin>unzip -t DilbertComic.jar
    > C:\bin>unzip -p DilbertComic.jar META-INF/MANIFEST.MF


    DilbertComic.jar? Reminds me of an old issue of Java Specialists'
    Newsletter...
     
    KC Wong, May 14, 2004
    #8
    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. Arnold Peters
    Replies:
    0
    Views:
    611
    Arnold Peters
    Jan 5, 2005
  2. muttley
    Replies:
    0
    Views:
    2,767
    muttley
    Oct 20, 2005
  3. cyberco
    Replies:
    4
    Views:
    3,844
    Roedy Green
    Feb 14, 2006
  4. Jason Cavett
    Replies:
    8
    Views:
    641
    Sanjay
    May 16, 2007
  5. Replies:
    0
    Views:
    454
Loading...

Share This Page