In this second part of a three-part series on using Java with deployment frameworks, you will learn how to deploy applets in a self-contained manner, and what they are and are not typically permitted to do once they are downloaded. This article is excerpted from chapter two of Advanced Java Game Programming, written by David Wallace Croft (Apress; ISBN: 1590591232).
Deploying Java Applets - Deploying as an Executable JAR (Page 2 of 5 )
Besides applet distribution, you can also use JAR files as self-contained executables. The user simply copies the JAR file to the desktop or a directory on the hard drive and it is ready to run. If the executable is a Swing application, the user double-clicks the JAR and it launches the program in a new window. If the executable is a command-line application, the user enters thejavacommand with the-jaroption.
Double-clicking or using the command-line-jaroption to launch the game is much easier on the player than specifying theclasspathand class name with the full package-name prefix on the command line. Contrast the two precedingjavacommands, one that uses the -jaroption and the other that does not.
public static void main ( String [ ] args )
When players launch an executable JAR file, thejavacommand framework looks for a staticmain()method within a designated class and calls it. This causes your game to start running. In this sense themain()method can be considered a lifecycle method. I realize that I am stretching this a bit.
Making the Manifest
A JAR file often contains many classes, and more than one of these classes could have amain()method. To distinguish which class should have itsmain()method launched, thejavacommand looks for aMain-Classentry in the manifest file.
The preceding code is a line in a manifest file, possibly the only line. It is written in the standard name-value pair format commonly used in property files before XML came about. You separate the class name value from the property name by a colon and a space.
CAUTION Keep in mind that only one space can follow the colon. If you put two spaces before the class name, it simply does not work.
jar -cvfm collection.jar manifest.txt .
Your JAR file normally includes a blank manifest file automatically asMETA-INF/MANIFEST.MFwhen you use thejarcommand. To include a non-empty manifest file that contains yourMain-Classentry, you need to use the-moption to thejarcommand. In the above example, thejarcommand is instructed to include the text filemanifest.txt. Whenmanifest.txtis included, it is automatically namedMANIFEST.MFand stored in theMETA-INFdirectory within the archive.
In an Ant build file, you use themanifestattribute to specify the path to the manifest file to include.
Securing the Insecure
Executable JAR files generally run on the client platform with unrestricted access to sensitive resources such as the hard drive and Internet connections by default. This can make some knowledgeable users leery of playing games distributed as executable JARs, especially when they do not know the distributor well. Such users would generally prefer to play games that are automatically restricted to a security sandbox such as those distributed as a browser applet or as a Java Web Start application.
java -Djava.security.manager -jar collection.jar
You can specify the security manager to use when you run JAR files using thejavacommand propertyjava.security.manager. In the preceding command-line example, no value for this property is given so the restrictions that would be imposed upon an unsigned applet running in a browser are enforced upon the executable JAR. If you use this property, you should be safe. Keep in mind that the game might throw someSecurityExceptions and fail to run properly if it was not designed to run within a security sandbox.