Home arrow Java arrow Page 2 - Deploying Java Applets
JAVA

Deploying Java Applets


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).

Author Info:
By: Apress Publishing
Rating: 4 stars4 stars4 stars4 stars4 stars / 20
April 17, 2008
TABLE OF CONTENTS:
  1. · Deploying Java Applets
  2. · Deploying as an Executable JAR
  3. · Deploying with Java Web Start
  4. · Accessing the Default Browser
  5. · Dynamic Linking Using Reflection

print this article
SEARCH DEVARTICLES

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.

java -jar fraction.jar
java -cp J:\lib com.croftsoft.apps.fraction.FractionAction

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.

Main-Class: com.croftsoft.apps.fraction.FractionAction

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.

  <jar
    jarfile="collection.jar"
    basedir="jar"
    manifest="bld/apps/collection/ manifest.txt"/>

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.


blog comments powered by Disqus
JAVA ARTICLES

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials