Home arrow Java arrow Page 4 - Deployment Frameworks
JAVA

Deployment Frameworks


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

Author Info:
By: Apress Publishing
Rating: 4 stars4 stars4 stars4 stars4 stars / 11
April 12, 2005
TABLE OF CONTENTS:
  1. · Deployment Frameworks
  2. · Reading from a JAR File
  3. · Signing Applets
  4. · Deploying with Java Web Start
  5. · Isolating Optional Packages
  6. · Deploying Multiple Applets as One
  7. · Lifecycle
  8. · MultiApplet
  9. · CroftSoftCollection

print this article
SEARCH DEVARTICLES

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.

<?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"/>

</jnlp>

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.

  <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

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.

Method showDocument()

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.


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