Appendix A. Tackling New Features of Java 2 Version 1.4
Appendix A. Tackling New Features of Java 2 Version 1.4
Appendix A. Tackling New Features of Java 2 Version 1.4
Every new edition of Java 2 includes dozens of new features, so it's always a challenge to cover them
in 24 hours.
Many new features are tailored toward advanced programming, putting them a bit beyond the scope
of a book such as this one. However, there are always some new classes, methods, and techniques that
new and intermediate-skilled Java programmers should know about, such as the addition of assertions
in version 1.4, which was covered during Hour 18, "Handling Errors in a Program."
Another is the addition of Java Web Start, which enables you to install and run applications by
clicking a link in a Web browser. It's an amazing new feature of the language that will change the way
programmers distribute applications, but it couldn't be shoehorned into the first 24 hours. So, I'm
going into at least 40 minutes of overtime by covering the following topics:
One of the issues you must deal with as a Java programmer is how to make your software available to
the people who will be using your work.
Applications require a Java interpreter, so one must either be included with the application, or users
must install an interpreter themselves. The easiest solution (for you) is to require that users download
and install the Java Runtime Environment from Sun's Web site at http://java.sun.com/getjava/.
Regardless of how you deal with the requirement for an interpreter, you distribute an application like
any other program, making it available on a CD or Web site, or by some other means. A user must run
an installation program to set it up, if one is available, or copy the files and folders manually.
Applets are easier to make available, because they can be run by Web browsers such as Microsoft
Internet Explorer, Netscape Navigator, and Mozilla. However, if your program is a Java 2 applet,
users must be running browsers that are equipped with the Java Plug-in. This too can be downloaded
from Sun as part of the Java Runtime Environment.
There are several drawbacks to offering applets instead of applications, as detailed during Hour 17,
"Creating Interactive Web Programs." The biggest is the default security policy for applets, which
makes it impossible for them to read and write data on a user's computer, among other restrictions.
In Java 2 version 1.4, Sun offers support for an alternative means of distribution: Java Web Start, a
method of downloading and running applications by clicking links on Web pages.
Java Web Start, which requires the Java Runtime Environment, offers a way to install and run
applications as if they were applets. Here's how it works:
A programmer packages an application into a JAR file along with a file that uses the Java
Network Launching Protocol (JNLP), part of Java Web Start.
The file is published on a Web server along with a Web page that links to that file.
A user loads the page with a browser and clicks the link.
If the user does not have the Java Runtime Environment, a dialog box opens asking if it
should be downloaded and installed. The full installation is currently 5.7MB in size, which might
take 30-45 minutes to download on a 56k Internet connection.
The Java Runtime Environment installs and runs the program, opening new frames and other
interface components like any other application. The program is saved in a cache, so it can be run
again later without requiring another installation.
To see it in action, visit Sun's Java Web Start site at http://java.sun.com/products/javawebstart and
click the Demos link. The Web Start Demos page contains pictures of several Java applications, each
with a Launch button you can use to run the application, as shown in Figure A.1.
Click the Launch button of one of the applications. If you don't have the Java Runtime Environment
yet, a dialog box opens asking whether you'd like to download and install it.
The runtime environment includes the Java Plug-in, a Java interpreter that adds support for the current
version of the language to browsers such as Internet Explorer and Mozilla. The environment can also
be used to run applications, whether or not they make use of Java Web Start.
When you run an application using Java Web Start, a title screen will be displayed on your computer
briefly and the application's graphical user interface will appear.
Note
If you installed the Java 2 Software Development Kit or tested Java 2 applets during Hour 17, you are
likely to have the Java Runtime Environment on your computer already.
Figure A.2 shows one of the demo applications offered by Sun, a military strategy game in which
three black dots gang up on a red dot and attempt to keep it from moving into their territory.
As you can see in Figure A.2, the application looks similar to the applications you created during the
preceding 24 hours. Unlike applets, which are presented in conjunction with a Web page, applications
launched with Java Web Start run in their own windows, as if they were run from a command line.
If you try out the dot game, be advised that the red dot is a lot tougher than he looks, even when
outnumbered. I tried it out numerous times during the preparation of this part of the book, and in
every single game my black dots were trounced. They did so badly I wanted to call in reinforcements.
One thing that's different about a Java Web Start application is the security that it offers to users.
When an application attempts to do some things such as reading and writing files, Java Web Start can
ask the user for permission.
For example, another one of the demo programs is a text editor. When you try to save a file with this
application for the first time, the Security Advisory dialog box opens (Figure A.3).
Once Java Web Start runs an application, it is stored in a cache, enabling you to run it again later
without having to install it. The only exception is when a new version of the application has become
available. In this circumstance, the new version will be automatically downloaded and installed in
place of the existing one.
Although you run a Java Web Start application for the first time using a Web browser, you don't have
to use a browser every time you run it. To see this, run one of the demo programs such as the dot
strategy game several times. A dialog box will appear asking if you want to create shortcuts to run it
without a browser (Figure A.4).
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.
The special file that you must create 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.
Note
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 (which is J24work if you followed one of the suggestions in
the book). 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:
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 Web site. Go to http://www.java24hours.com and open the Appendix A 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 A.1
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.
The structure of a JNLP file is similar to the HTML markup that was required in Hour 17 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.
<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. I've used my name
here. Delete it and replace it with your own name, taking care not to alter the <vendor> or </vendor>
tags around it.
<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 were 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 can also have attributes, which provide 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.
<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 A.1 defines other information
required by Java Web Start.
Line 1 indicates 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: Java 2 version 1.4.
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 A.1 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 will begin running, as in Figure A.5.
If your server does not support Java Web Start, which is more likely than not given the fact that 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
If you can't get Java Web Start working on your server, you can test this project on the book's official
site. Load the Web page http://www.cadenhead.org/books/java24hours/java/LottoMadness.jnlp, or
save your carpal tunnel nerves and visit the Web address http://www.java24hours.com and open the
Appendix A page.
Note
Java Web Start applications should run exactly as they do when run by other means. However, there
appear to be a few bugs in how much space is allocated to components on a graphical user interface.
On a Windows system, you might need to add 50 pixels to the height of the LottoMadness application
before employing it in Java Web Start. Otherwise, the text fields are not tall enough to display
numbers.
Summary
At the end of Hour 24, we said farewell to each other, exchanging poignant goodbyes like a couple of
friends at their high school graduation who are going to different colleges.
You probably didn't expect to hear from me again so soon. The name of the book isn't Teach Yourself
Java 2 in 24 Hours and 40 Minutes, after all.
However, Java Web Start is such an exciting way to distribute Java applications that I felt that it was
worth bending the rules a little. Java Web Start itself bends the rules, running applications as if they
were applets, then escaping the browser entirely, running applications using shortcuts that keep
themselves up-to-date using the World Wide Web.
The project you completed in this appendix used the most basic configuration options in JNLP: the
XML file format used to define and set up Java Web Start. It can also be used to change the title
graphic that appears when the application is launched, run signed applications that have different
security privileges, run an application using different versions of the Java interpreter, and other
options.
For more information on making use of the technology with your own applications, visit Sun's Java
Web Start site at the address
http://java.sun.com/products/javawebstart/1.2/docs/developersguide.html.