Once you have learned how to program games in Java, you will want to be able to deploy them on different types of deployment frameworks. This article will cover browser applets, executable JARs, and Java Web Start. It is excerpted from the book Advanced Java Game Programming written by David Wallace Croft (Apress, 2004; ISBN 1590591232).
Deployment Frameworks - Deploying with Java Web Start (Page 4 of 9 )
Deploying with Java Web Start
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.
The preceding code is the content of the collection.jnlp file 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 archive collection.jar. The next-to-last line designates the class containing the main() 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 .htaccess and you might need it if you are using a web server that is not already configured to associate files automatically with the .jnlp file name extension with the MIME type application/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 .jnlp file 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 .jnlp file as a plain-text file instead of launching the JNLP client, you probably forgot too. Try uploading .htaccess and 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 .jnlp file 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.
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
Accessing the Default Browser
Java desktop applications sometimes need to present the user with online web pages for news, documentation, and registration forms. Although Java currently supports the limited ability to display HTML using class javax. swing.JEditorPane, web browsers such as Netscape or Internet Explorer have additional advanced capabilities such as supporting HTML scripting languages and various multimedia formats. This section describes how to enable your Java desktop applications to launch the default web browser external to your application on the client platform in a platform-independent manner.
Java has always supported the ability of an applet to control its browser container via the showDocument() method of interface java.applet.AppletContext. Unfortunately, Java desktop applications, as opposed to applets, do not have access to instances of AppletContext. The JNLP API provides an interface— javax.jnlp.BasicService—which provides a similar method for JNLP desktop applications.
This article is excerpted from Advanced Java Game Programming by David Wallace Croft (Apress, 2004; ISBN 1590591232). Check it out at your favorite bookstore today. Buy this book now.