Using Java Web Start





Using Java Web Start

Any Java application can be run using Java Web Start, as long as the web server that will offer the application is configured to work with the technology.

To prepare an application to use Java Web Start, you must save the application's files in a Java Archive (JAR) file, create a special Java Web Start file for the application, and upload the files to the web server. Use java file upload capability.

The special file that must be created uses Java Network Launching Protocol (JNLP), an XML file format that specifies the application's main class file, its JAR archive, and other things about the program.

By the Way

XML, which stands for Extensible Markup Language, is a way of describing data that's similar in some ways to HTML, the language used to create web pages. You don't need to know anything about XML to create a JNLP file, because the format of JNLP files is relatively self-explanatory.


The next project you will undertake is to use Java Web Start to launch and run the LottoMadness application from Hour 15, "Responding to User Input." To get ready, put a copy of that project's files in your main Java programming folder. The files you need are LottoEvent.class and LottoMadness.class, though you might also want LottoEvent.java and LottoMadness.java, in case you decide to make any changes to the application.

The first thing you must do is package all of an application's class files into a Java Archive (JAR) file along with any other files it needs. If you are using the Software Development Kit, you can create the JAR file with the following command:

jar -cf LottoMadness.jar LottoEvent.class LottoMadness.class

A JAR file called LottoMadness.jar is created that holds both of the class files.

Next you should create an icon graphic for the application, which will be displayed when it is loaded and used as its icon in menus and desktops. The icon can be in either GIF or JPEG format, and should be 64 pixels wide and 64 pixels tall.

For this project, if you don't want to create a new icon, you can download lottobigicon.gif from the book's website. Go to http://www.java24hours.com and open the Hour 17 page. Right-click the lottobigicon.gif link and save the file to the same folder as your LottoMadness.jar file.

The final thing you must do is to create the JNLP file that describes the application. Listing 17.5 contains a JNLP file used to distribute the LottoMadness application. Open your word processor and enter the text of this listing, then save the file as LottoMadness.jnlp.

Listing 17.5. The Full Text of LottoMadness.jnlp
 1: <?xml version="1.0" encoding="utf-8"?>
 2: <!-- JNLP File for LottoMadness Application -->
 3: <jnlp
 4:   codebase="http://www.cadenhead.org/book/java24hours/java"
 5:   href="LottoMadness.jnlp">
 6:   <information>
 7:     <title>LottoMadness Application</title>
 8:     <vendor>Rogers Cadenhead</vendor>
 9:     <homepage href="http://www.java24hours.com"/>
10:     <icon href="lottobigicon.gif"/>
11:     <offline-allowed/>
12:   </information>
13:   <resources>
14:     <j2se version="1.5"/>
15:     <jar href="LottoMadness.jar"/>
16:   </resources>
17:   <application-desc main-class="LottoMadness"/>
18: </jnlp>

The structure of a JNLP file is similar to the HTML markup required to put a Java applet on a web page. Everything within < and > marks is a tag, and tags are placed around the information the tag describes. There's an opening tag before the information and a closing tag after it.

For example, Line 7 of Listing 17.5 contains the following text:

<title>LottoMadness Application</title>

In order from left to right, this line contains the opening tag <title>, the text LottoMadness Application, and the closing tag </title>. The text between the tags—LottoMadness Application— is the title of the application, which will be displayed when it is loaded and used in menus and shortcuts.

The difference between opening tags and closing tags is that closing tags begin with a slash (/) character and opening tags do not. In Line 8, <vendor> is the opening tag, </vendor> is the closing tag, and these tags surround the name of the vendor who created the application. Place your own name within these tags, taking care not to alter the <vendor> or </vendor> tags around it.

Some tags have an opening tag only, such as Line 11:

<offline-allowed/>

The <offline-allowed/> tag indicates that the application can be run even if the user is not connected to the Internet. If it was omitted from the JNLP file, the opposite would be true, and the user would be forced to go online before running this application.

In XML, all tags that do not have a closing tag end with /> instead of >.

Tags also can have attributes, which are another way to define information in an XML file. An attribute is a word inside a tag that is followed by an equals sign and some text within quotes.

For example, consider Line 9 of Listing 17.5:

<homepage href="http://www.java24hours.com"/>

This is the <homepage> tag, and it has one attribute, HRef. The text between the quote marks is used to set the value of this attribute to http://www.java24hours.com. This defines the home page of the application—the web page that users should visit if they want to read more information about the program and how it works.

The LottoMadness JNLP file defines a simple Java Web Start application that does not make use of any Java features that require special permission.

In addition to the tags that have already been described, Listing 17.5 defines other information required by Java Web Start.

Line 1 designates that the file uses XML and the UTF-8 character set. This same line can be used on any of the JNLP files you create for applications.

Line 2 is a comment. Like other comments in Java, it's placed in the file solely for the benefit of humans. Java Web Start will ignore it.

Lines 3–5 indicate where the JNLP file for this application can be found. The codebase attribute in Line 4 is the URL of the folder that contains the JNLP file. The HRef attribute in Line 5 is the name of the file. These lines indicate that the file is at the web address http://www.cadenhead.org/book/java24hours/java/LottoMadness.jnlp.

Lines 6 and 12 use the <information> and </information> tags to surround information about the application. Tags can contain other tags in XML: This tag contains <title>, <vendor>, <homepage>, <icon/>, and <offline-allowed/> tags.

Line 10 indicates the location of the program's icon, which uses the codebase attribute described previously. In this example, the file is at the web address http://www.cadenhead.org/book/java24hours/java/lottobigicon.gif.

Lines 13 and 16 use the <resources> and </resources> tags to surround information about resources used by the application.

Line 14 indicates the version of the Java interpreter that should run the application: 1.5, which sets up the application to use Java 2 version 5 (which has the internal version number 1.5)

Line 15 indicates the application's JAR file. This also uses codebase, so this file is in http://www.cadenhead.org/book/java24hours/java/LottoMadness.jar.

Line 17 indicates which class file should be run to start the application: LottoMadness. Note that the .class file extension is not specified—only the name of the class itself.

Line 18 ends the definition of this JNLP file. All JNLP files must be contained within an opening <jnlp> tag and a closing </jnlp> tag.

After you have created this file, change Line 4 of Listing 17.5 so that it refers to the folder on a web server where your application's JAR file, icon file, and JNLP file will be stored.

Upload all three of the files to this folder, then run your browser and load the JNLP file using its full web address. If your web server is configured to support Java Web Start, the application will be loaded and begin running, as in Figure.

Figure. Running LottoMadness using Java Web Start.


If your server does not support Java Web Start, which is more likely than not because it is a relatively new technology, you may see the text of your JNLP file loaded in a page and the application will not open.

A web server must be configured to recognize that JNLP files are a new type of data that should be run as an application, not delivered to the user as text in a browser window. On an Apache web server, the server administrator can support JNLP by adding the following line to the server's mime-types (or .mime-types) file:

application/x-java-jnlp-file JNLP

Watch Out!

If you can't get Java Web Start working on your server, you can test this project on the book's official site. Visit the web page http://www.java24hours.com, open the Hour 17 page, and click the LottoMadness.jnlp link.



     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows