Previous Next |
Using Java Web StartAny Java app can be run using Java Web Start, as long as the web server that will offer the app is configured to work with the technology. To prepare an app to use Java Web Start, you must save the app's files in a Java Archive (JAR) file, create a special Java Web Start file for the app, and upload the files to the web server. The special file that must be created uses Java Network Launching Protocol (JNLP), an XML file format that specifies the app's main class file, its JAR archive, and other things about the program.
The next project you will undertake is to use Java Web Start to launch and run the LottoMadness app from Hour 15, "Responding to User Input." To get ready, put a copy of that project's files in your main Java coding 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 app. The first thing you must do is package all of an app'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 app, 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 tutorial'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 app. Listing 17.5 contains a JNLP file used to distribute the LottoMadness app. 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.jnlp1: <?xml version="1.0" encoding="utf-8"?> 2: <!-- JNLP File for LottoMadness app --> 3: <jnlp 4: codebase="http://www.cadenhead.org/book/java24hours/java" 5: href="lottomadness.jnlp"> 6: <information> 7: <title>LottoMadness app</title> 8: <vendor>Rogers Cadenhead</vendor> 9: <homepage href="http://web.archive.org/web/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: <app-desc main- /> 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 app</title> In order from left to right, this line contains the opening tag <title>, the text LottoMadness app, and the closing tag </title>. The text between the tags—LottoMadness app— is the title of the app, 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 app. 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 app 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 app. 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://web.archive.org/web/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 app—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 app 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 apps. 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 app 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 app. 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 app. Line 14 indicates the version of the Java interpreter that should run the app: 1.5, which sets up the app to use Java 2 version 5 (which has the internal version number 1.5) Line 15 indicates the app'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 app: 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 app'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 app will be loaded and begin running, as in Screenshot. Screenshot 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 app 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 app, 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: app/x-java-jnlp-file JNLP
|
Previous Next |