8.2 Java Web Start
Java Web Start is a new standard for distributing Java applications, based on a blend between applet advantages (delivered and automatically updated via the Web) and standalone applications (without any dependencies on a web browser). The documentation, several
As shown in Figure 8-7, users will typically first encounter a Web Start application while browsing the Web. Clicking on a link to a Web Start JNLP file causes the browser to launch a helper application, which in
Figure 8-7. Web Start delivery
Consider, for example, a Tic-Tac-Toe game in which the
At the heart of Web Start is the JNLP file (
Before starting, it's worth spending some time looking at the Web Start management application stored in /Applications/Utilities/Java (shown in Figure 8-8). This is the only real user interface to Web Start beyond whatever interface you present as part of your application.
Figure 8-8. The
When building JNLP-based applications, consider the restrictions on the environment and the packaging. From Sun's Web Start documentation (at http://java.sun.com/products/javawebstart/1.2/docs/developersguide.html#dev), the following list details the required attributes of JNLP-delivered applications:
An application must be delivered as a set of JAR files.
All application resources, such as files and images, must be stored in JAR files, and they must be referred to by using the getResource( ) mechanism in the Java 2 platform (see Section 8.2.3 below).
An application is allowed to use the System.exit( ) call.
An application that needs unrestricted access to the system must be delivered in a set of signed JAR files. All entries in each JAR file must be signed.
If an application is written to run in a secure sandbox, it must follow these additional restrictions:
No access to local disk is available.
All JAR files must be downloaded from the same host.
Network connections are enabled only to the host from which the JAR files are downloaded.
No security manager can be installed.
No native libraries can be installed or utilized.
Limited access is provided to system properties. The application has read/write access to all system properties defined in the JNLP file, as well as read-only access to the same set of properties that an applet has access to.
You need to be aware of only a few differences between the Mac OS X implementation of Java Web Start and that of the Windows and Solaris versions. First, Mac OS X does not support dynamic downloading of additional Java Runtime Environments (JREs). Mac OS X includes J2SE 1.4 (and 1.3.1 is easily available), so if your application specifically requires JRE 1.2 or prior, it will not work. Users who need the latest JVM should use their standard Mac OS X Software Update functionality (available in System Preferences) to download Apple JDK releases. Specifications for version
Java Web Start maintains strict control over the class loading configuration. It transfers JAR files from the web server to the client machine, and chooses where to store the JAR files; an application cannot use disk-relative references to resources such as images and configuration files.
Therefore, application resources should be retrieved from the JAR files specified in the resources section of the JNLP file, or retrieved explicitly by an HTTP request to the web server. It's
The code example shown in Example 8-3 shows how to retrieve images from a JAR file. The example assumes that the entries images/save.gif and images/cut.gif exist in the application's JAR files.
// Get current classloader ClassLoader cl = this.getClass().getClassLoader( ); // Create icons Icon saveIcon = new ImageIcon(cl.getResource("images/save.gif")); Icon cutIcon = new ImageIcon(cl.getResource("images/cut.gif"));
Developers sign code for use with Java Web Start much like they do for Java applets: by using the standard
In this case, you'll use the built-in Mac OS X implementation of Apache to serve JNLP files. To do this, you need to add the line shown below to your mime.types file, located in /etc/httpd/ .
Using this implementation will ensure that Web Start applications are associated with JNLP and the proper programs on your Mac OS X machine. The easiest way to ensure this is to use a one-time execution of a command-line text editor from within Terminal:
cd /etc/httpd/ sudo pico mime.types
You'll need to enter your password, and you'll see the text editor shown in Figure 8-10. Make the needed changes and quit out of Pico.
You'll then want to restart Apache in the "System Preferences Sharing" control panel (as shown back in Figure 8-2).
Before building a Web Start-based application, you'll need a JAR file. In this case, use the JAR file built in Chapter 7. You've been getting some mileage out of this application, as it was already deployed earlier in this chapter as an applet.
Then create a JNLP configuration text file, as shown in Example 8-4. Save this file as SimpleEdit.jnlp in your ~/Sites directory.
<?xml version="1.0" encoding="utf-8"?> <!-- JNLP File for SimpleEdit --> <jnlp spec="1.0+" codebase="http://127.0.0.1/~wiverson/" href="SimpleEdit.jnlp"> <information> <title>SimpleEdit</title> <vendor>Will Iverson</vendor> <homepage href="http://127.0.0.1"/> <description>An extremely minimal text editor</description> <description kind="short">An extremely minimal (but extensible) text editor. </description> <icon href="images/notset.jpg"/> <icon kind="splash" href="images/notset.gif"/> <offline-allowed/> </information> <resources> <j2se version="1.3"/> <jar href="SimpleEdit.jar"/> </resources> <application-desc main-class="com.wiverson.macosbook.SimpleEdit"/> </jnlp>
Next, create a simple HTML file to link to this JNLP file (so users have something to click on in their web browser). Save the contents of Example 8-5 as SimpleEditWebStart.html in the ~/Sites directory.
<HTML> <HEAD> <TITLE>SimpleEdit WebStart</TITLE> </HEAD> <BODY> <A HREF="SimpleEdit.jnlp">Launch SimpleEdit</A> </BODY> </HTML>
You should now be able to view the HTML page in your web browser by viewing http://127.0.0.1/~username/SimpleEditWebStart.html . Clicking on the "Launch SimpleEdit" link will, depending on your web browser, prompt the user to save the JNLP file to disk or automatically launch Web Start. If the file is saved to disk, the user can then launch Web Start (and the SimpleEdit application) by double-clicking on the JNLP file.
As you can see from the example, a JNLP file is a standard XML file. Most of the information contained in the file is
The base URL for all relative HREF URLs in the rest of the JNLP file. If your test system is
but your production system is
This attribute's value should be set to the
This element should
This URL appears automatically in the Java Web Start management application as a clickable link.
This graphic is automatically converted to a local system icon if users save an application to their system. The graphic should be square, 64 x 64 pixels (Java Web Start will automatically resize this graphic as needed).
If an icon with the splash attribute is provided, this icon will be used when Java Web Start downloads or updates the application.
This tag must be present if the application is to be launched when not connected to the network. There are no configurable options; for more sophisticated control, you will probably want to include this option and then perform network availability checks within your application.
You should include one or more of these entries to refer to the various JAR files required by your application.
This entry is used to specify the main class for your application. Subentries can be provided to pass arguments to the application via argument tags. For example, if you wanted to load a SimpleEdit plug-in, you might pass in an argument as shown below:
<application-desc main-class="com.wiverson.macosbook.SimpleEdit"> <argument> com.wiverson.macosbook.webservices.XmlRpcAsynchClientPlugin </argument> </application-desc>