In LearningJava I remarked that javac doesn't produce executables. The way that this is done is to create a JAR file which contains all the class files together with other resources needed. Does this mean that you can include classes from library packages so that the end user need not have the library installed?
JAR Files
Using JAR Files: The Basics
JAR files are created using the jar program. To run an application stored in a JAR you use the java program but this time the arguments are more complex:
java -jar app.jar
What I want to do is package everything that the application needs (except the JRE) into a single JAR so that deployment is simple.
Creating a JAR File
Here we learn how to put files that are not Java classes in the JAR.
jar cvf TicTacToe.jar TicTacToe.class audio imagesThis line ceates TicTacToe.jar, adds the TicTacToe.class file to it along with all the files in the audio and images sub-directories.
The switches simply mean:
- c
- create a jar file
- v
- give verbose messages
- f
- send the JAR file to a file instead of standard out.
An important point to note is that the JAR preserves the directory structure.
A manifest (META-INF/MANIFEST.MF) file that lists all the files is produced automatically and included in the JAR.
Understanding the Manifest
At last we find a section on packaging application in JARs and how to do it. The key is to provide a manifest instead of letting the jar program create it for us. The manifest must specify the name of the class that contains the main entry point:
Main-Class: classnamewhere classname is the name of the class containing the
main
function.
To inlude this information in the JAR you must add it to the jar command line:
jar cmf mainClass app.jar HelloWorld.class
Manifest Format
Of course there is much more to the manifest. Unfortunately the Manifest Format page doesn't explain much of it.
Thinlet JARs
The Sun, Using JAR Files: The Basics, tutorial doesn't say much about what you can put in a JAR or how; all that is on other pages. The Thinlet site has a very neat page on the subject as you would use it for packaging a program and the library code it relies on. Click on the section heading to see it.
Jar Files
Here is another page on JARs from what seems to be an online book.
Viewing the Contents of a JAR File
If, like me, you are forgetful and easily confused it can sometimes be a good idea to look inside the JAR to see if what is in there is what you intended and if it is in the expected places.
jar tf jar-file
Running JAR-Packaged Software
JAR Files as Applications - 1.2 platform only
For this to work you must modify the manifest so that it tells which class contains the main entry point. I've mentioned this before.
No comments:
Post a Comment