Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   How to create an executable jar (http://www.velocityreviews.com/forums/t133475-how-to-create-an-executable-jar.html)

Frank Burns 05-13-2004 09:27 PM

How to create an executable jar
 
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



Chris Smith 05-13-2004 09:51 PM

Re: How to create an executable jar
 
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 05-13-2004 11:33 PM

Re: How to create an executable jar
 
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" <cdsmith@twu.net> wrote in message
news:MPG.1b0d9be7ede1bc9b989aad@news.pop4.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




Roedy Green 05-14-2004 12:01 AM

Re: How to create an executable jar
 
On Thu, 13 May 2004 22:27:56 +0100, "Frank Burns"
<frankburns@NOSPAMthe-hub.demon.co.uk> wrote or quoted :

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


see http://mindprod.com/jgloss/associations.html
and
http://mindprod.com/jgloss/jar.html

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.

KC Wong 05-14-2004 01:21 AM

Re: How to create an executable jar
 
> 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}



Chris Smith 05-14-2004 02:52 AM

Re: How to create an executable jar
 
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

Liz 05-14-2004 05:22 AM

Re: How to create an executable jar
 

"KC Wong" <my@privacy.net> wrote in message
news:2gil98F3ek8oU1@uni-berlin.de...
> > 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



KC Wong 05-14-2004 05:46 AM

Re: How to create an executable jar
 
> 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...




All times are GMT. The time now is 02:07 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.