Java
  Home arrow Java arrow Page 3 - Deploying Java Applets
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Deploying Java Applets
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 8
    2008-04-17

    Table of Contents:
  • Deploying Java Applets
  • Deploying as an Executable JAR
  • Deploying with Java Web Start
  • Accessing the Default Browser
  • Dynamic Linking Using Reflection

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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

    More Java Articles
    More By Apress Publishing


       · This article is an excerpt from the book "Advanced Java Game Programming," published...
     

    Buy this book now. This article is excerpted from chapter two of Advanced Java Game Programming, written by David Wallace Croft (Apress; ISBN: 1590591232). Check it out today at your favorite bookstore. Buy this book now.

    JAVA ARTICLES

    - 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 ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...







    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 9 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek