How to create an executable jar

F

Frank Burns

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
 
C

Chris Smith

Frank said:
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
 
M

Mike Smith

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
 
K

KC Wong

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}
 
C

Chris Smith

Mike said:
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
 
L

Liz

KC Wong said:
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
 
K

KC Wong

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...
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top