The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
September 2000

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

How : Making a JAR file

Posted by Kumar on October 15, 2001 at 5:00 AM

> > I have programmed a java file .The program includes seven
> > java files.The program runs well,but I want to make these
> > files a executable jar file. So I just click the name of the
> > jar file,the applet will run.
> > please tell me how?
> > thanks

To do this you must create your own manifest file and then use this manifest file when you create your jar file. A manifest file is simply a plain text file with some special tags that the Java virtual machine knows how to work with. You can include special tags in the mainfest file that tell the Java virtual machine which file in that jar file is the executable "main" class, as well as specifying a classpath. As of RePast 1.3 all the demonstration simulations are run in this way. The following is the manifest file from bugs.jar (the heatbugs demo simulation).
Main-Class: uchicago.src.sim.heatBugs.HeatBugsModel
Class-Path: ../../lib/repast.jar ../../lib/trove.jar ../../lib/colt.jar
../../lib/jgl3.1.0.jar ../../lib/excelaccessor_Runtime.jar ../../lib/jmf.jar
../../lib/jcchart.jar ../../lib/junit.jar ../../lib/plot.jar
../../lib/xerces.jar

The Class-Path: entries should all be one line. Here the main class is uchicago.src.sim.heatBugs.HeatBugsModel and it requires the specified classpath in order to execute correctly. The class path is space delimited here, and the paths of the jar files are defined relative to the bugs.jar file. In this case two directories above the repast/demo/bugs directory in the repast/lib directory. With this manifest file in the bugs.jar archive, we execute the heatbugs simulation with java -jar bugs.jar. This has the added benefit of making the jar file executable when double click under windows. Given the above manifest file, this is directly equivalent to:
java -cp ./bugs.jar;../../lib/repast.jar;../../lib/trove.jar;
../../lib/colt.jar; ../../lib/jgl3.1.0.jar;
../../lib/excelaccessor_Runtime.jar;../../lib/jmf.jar;
../../lib/jcchart.jar;../../lib/junit.jar;../../lib/plot.jar;
../../lib/xerces.jar uchicago.src.sim.heatBugs.HeatBugsModel

(Note that as of RePast v.1.3, you no longer start models this way. repast.jar itself contains a manifest file that specifies a classpath containing all the other required jar files. So instead of the above, you can do java -cp ./bugs.jar;../../lib/repast.jar uchicago.src.sim.heatBugs.HeatBugsModel.)
Once you've written your manifest file, you need to include it in your jar. If you haven't yet created your jar file, you can do this all in one step with the 'jar' utility that comes with the java development kit.

jar cfvm my_model.jar mymanifest my_model_dir

where my_model.jar is the name of the jar file you are creating, mymanifest is the manifest file you've created and my_model_dir is the directory for your actual model. So for heatbugs this looks like:
jar cfvm bugs.jar bugs_manifest uchicago/src/sim/heatBugs

If your model is in a package (as it should be) its important to jar it from the directory above the first package directory. In the heatbugs case I would run the jar command from the directory in which the uchicago directory resided.
If you already have your classes in a jar file and just want to add the manifest file, replace the 'c' in the above command with a 'u'.

You can inspect the contents of your jar file with jar -tf name_of_jar_file. Your manifest won't be included in the jar itself as its contents are actually copied into the MANIFEST.MF file.





Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us