Home arrow Java arrow Page 3 - 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 with Java Web Start
(Page 3 of 5 )

Java Web Start has all the advantages of browser-based applet distribution and persistent desktop-application distribution without the disadvantages. When users visit your web page, they are prompted to download your game and run it just like a desktop application. Because the game is persistently installed on the client machine, they can play it without an Internet connection in the future. Whenever you revise the code on your web page, Java Web Start updates the user client installations automatically with the latest version. Users can feel comfortable downloading and running these games because they run within a security sandbox just like an applet. Unlike an applet, you do not need server-side code to centrally store user game data because the Java Web Start sandbox is loose enough to allow you to persist a small amount of game data in a manner akin to browser cookies.

For all these reasons, Java Web Start is, in my opinion, the best way to distribute your Java games. As a player, I want to be able to play sophisticated and detailed Java games without having to download a huge JAR file each time I play. At the same time, I worry about giving programs written by strangers access to my hard drive. As a game developer, I want to be able to make great games and only pay the bandwidth distribution costs when I release a new version. Additionally, I do not want to support a cluster of servers just to persist single-player game data such as high scores and user-preference settings. Java Web Start gives me what I need in both roles.

Java Web Start is automatically installed when you install version 1.4 of the Sun Microsystems implementation of the Java Runtime Environment (JRE) for Windows. It is also bundled with their implementation of Java 1.4 for Linux but requires a separate manual installation. Apple pre-installs Java Web Start with its Macintosh OS X 10.1 operating system.

Java Web Start is the Sun Microsystems implementation of the Java Network Launching Protocol (JNLP) client specification. Because Java Web Start is based upon the JNLP standard, your Java Web Start-compatible games also run in the JNLP client frameworks implemented by other vendors. These include Open Source project implementations such as OpenJNLP.

Preparing the Distribution Files

Setting up your Java Web Start application for distribution requires that you upload a web page plus a number of supporting files in addition to your code. Although the JNLP specification and the Java Web Start developer guide covers these in detail (see the reference below), the following code gives you a quick introduction to what is required.

<html>
<body>
<a href="collection.jnlp">Install the CroftSoft Collection</a>
</body>
</html>

The preceding code is a simple HTML web page with a hyperlink that downloads and installs a Java Web Start application when the user clicks it.

<?xml version="1.0" encoding="UTF-8"?>

<jnlp
  spec="1.0+" 
  codebase="http://www.croftsoft.com/ portfolio/collection/install"
  href="collection.jnlp">

  <information>
    <title>CroftSoft Collection</title>
    <vendor>CroftSoft Inc</vendor>
    <description kind="one-line">Java programs from CroftSoft.</description>
    <description kind="short">
     
A collection of Java programs from CroftSoft.
    </description>
    <description kind="tooltip">CroftSoft Collection</description>
    <homepage href="http://www.croftsoft.com/portfolio/ collection/"/>
    <icon kind="default" href="default_icon.gif"/>
    <offline-allowed/>
  </information>

  <resources>
    <j2se version="1.4+" href="http://java.sun.com/products/autodl/j2se"/>
    <jar href="collection.jar"/>
  </resources>

  <application-desc main-class="com.croftsoft.apps.collection.CroftSoftCollection"/>

The preceding code is the content of thecollection.jnlpfile referenced by the web page. Like Ant, JNLP uses XML for configuration instructions. This XML file tells a JNLP client such as Java Web Start to prompt the user to install the latest release of Java if version 1.4 or later is not already on the client. It then launches the code in the archivecollection.jar. The next-to-last line designates the class containing themain()method used to launch the program.

AddType application/x-java-jnlp-file .jnlp

In addition to the JAR file containing your program and a desktop icon for your program once installed, you might need to include one additional file in your distribution directory. The preceding single-line file shown is named.htaccessand you might need it if you are using a web server that is not already configured to associate files automatically with the.jnlpfile name extension with the MIME typeapplication/x-java-jnlp-file. If your web service provider is unwilling to reconfigure its MIME type settings for you, you can place this file in your distribution directory as an override setting instruction if you are using the Apache Web Server. This is necessary because the browser uses the MIME type sent by the web server to identify which plug-in to use—in this case Java Web Start or another JNLP-compatible client.


CAUTION   Forgetting to reconfigure the web server so it associates the.jnlpfile name extension with the MIME type was a common stumbling block for me and the students in the course that I taught on Java game programming. If you click the hyperlink for your game and your browser displays the.jnlpfile as a plain-text file instead of launching the JNLP client, you probably forgot too. Try uploading.htaccessand see if this fixes the problem.


<security>
  <all-permissions/>
</security>

If you really need to, you can run your Java Web Start applications without being limited by the security sandbox. If your JAR file is digitally signed, you can insert XML code into your.jnlpfile like the preceding code. This gives your game free rein to read and write anywhere on the hard drive and make all the Internet socket connections it wants to anywhere.

  <signjar
    jar="mygame.jar"
    storepass="${password}"
    alias="myself
    keystore="myKeystore"
    keypass="${password}" />

You can automate digitally signing your JAR files using an Ant build instruction such as the preceding code. The purpose is to ensure that you are who you say you are before users make a decision to trust running your code without restraints on their systems. If you do not want to spend the money to get a certificate of identification from a trusted third party, you can self-sign the JAR file. When you self-sign, the JNLP client warns the users that it cannot authenticate the identity of the distributor and advises them of the security risk. It then prompts users to cancel or continue.

For additional information, please see the Java Network Launching Protocol & API Specification and the Java Web Start 1.4.2 Developer Guide.4


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