The Gamer Corner
There's no problem with the chat code, you've just been banned.
Lost your password?

Java Jar and Manifest

Java Jar and Manifest – October 28, 2008 12:47 PM (edited 10/28/08 8:47 AM)
Cuzzdog (1522 posts) Head of Gamer Corner R&D
Rating: Not Rated
Sometimes getting a Java application to pickup all the related materials at runtime can be a bit of a pain, so I thought I'd jot some notes from my own experience to maybe save someone (or myself) a bit of trouble down the line.

First of all, a jar file is a zip file of all the related resources for an application. When running an application from a jar, you'll need a way to specify which file is the main executable of the jar, or you might get a runtime error like "Cannot find Main class exception". To fix this, you can add a Manifest file to the jar.

The Manifest file is just a plain text file that the jar file can use as a directory structure to find additional resources when running. So, if your Main class relies on a jar file inside your jar file, and at runtime cannot find said jar file, adding the appropriate lookup to the Manifest can smooth those problems out. At the minimal, the Manfest file should point to where your Main class is located. To do that, simply add this to a plain text file:

Main-Class: <Name of Class>

Where name of class is the name of the class your Main resides in. Simply put "MyClass" and not "MyClass.class". And then you're good to go. When you do a jar of your application, jar it like so:

jar cvfm <Name Of Jar To Be Made> <Location of your Manifest Document> <List of files to be added to Jar>

To run your application, you can now do "java -jar MyJar.jar" and the class you set as the Main class in your manifest file will be called.

If you're having trouble locating resources inside your jar file, and can't get the manifest working right, an alternative would be to not include them in your jar, and instead add their location to the environment variable CLASSPATH.

Active Users: (guests only)
1 user viewing | Refresh