Chapter 1

Using Java Web Start

IN THIS CHAPTER

check Using Java Web Start to launch Java applications from a web page

check Creating a JNLP file to enable Java Web Start for an application

check Crafting a Java Web Start HTML file

check Changing security settings to allow Java Web Start

Java Web Start is a way to deploy Java applications via a web page. The web page can reside on the Internet or on a local company intranet. Either way, Java Web Start allows a user to run a Java program by browsing to a web page and clicking a link.

Java Web Start can only be used to deploy programs that have a graphical user interface (GUI). The best way to develop a GUI program is to use JavaFX, as described in Book 6. You can also deploy GUI programs developed using Swing, an older method of creating GUI programs that isn’t covered in this book.

In this chapter, you learn how to deploy a simple JavaFX application via Java Web Start.

Looking at a Simple JavaFX Program

The JavaFX application used to illustrate Java Web Start is called ClickMe. When run, it simply displays the dialog box shown in Figure 1-1. As you can see, the dialog box contains a button labeled “Click me please!” When the user clicks the button, the button’s label changes to “You clicked me!” If the user clicks again, the button reverts to “Click me please!” and the process repeats until the user closes the program by clicking the Close button (the X in the upper-right corner of the window).

image

FIGURE 1-1: The ClickMe program in action.

The operation of this program is explained in detail in Chapter 1 of Book 6. You don’t really need to know anything about how the ClickMe program works to deploy via Java Web Start, but for reference the source code for the program is shown in Listing 1-1. The code shown in this listing should be saved to a file named ClickMe.java. If you want to know more about the program before you learn how to deploy it using Java Web Start, please refer to Book 6, Chapter 1.

LISTING 1-1 The ClickMe Program

import javafx.application.*;
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.layout.*;
import javafx.scene.control.*;

public class ClickMe extends Application
{
public static void main(String[] args)
{
launch(args);
System.out.println("Done");
}

Button btn;

@Override public void start(Stage primaryStage)
{
// Create the button
btn = new Button();
btn.setText("Click me please!");
btn.setOnAction(e -> buttonClick());

// Add the button to a layout pane
BorderPane pane = new BorderPane();
pane.setCenter(btn);
// Add the layout pane to a scene
Scene scene = new Scene(pane, 300, 250);
// Add the scene to the stage, set the title
// and show the stage
primaryStage.setScene(scene);
primaryStage.setTitle("The Click Me App");
primaryStage.show();
}

public void buttonClick()
{
if (btn.getText() == "Click me please!")
{
btn.setText("You clicked me!");
}
else
{
btn.setText("Click me please!");
}
}

}

The ClickMe.java source file can be compiled to a Java Class file by using the following command from a command prompt:

javac ClickMe.java

The class file can then be used to create an executable JAR file using this command:

jar cf ClickMe.jar *.class

After the JAR file has been created, you’re ready to configure Java Web Start to launch the ClickMe program from a web page.

Understanding Java Web Start

Java Web Start works by providing a web link to a special file called a JNLP file. This file contains all the information necessary to download and launch a Java application on an end user’s computer. When the user clicks the link to the JNLP file, the web server reads the contents of the JNLP file, downloads the Java program, and runs it on the user’s computer.

With Java Web Start, the Java program does not run within the user’s web browser environment. Instead, Java Web Start runs the Java Virtual Machine (JVM) locally on the user’s computer, and then runs the application within the JVM.

technicalstuff An older technology called applets allows Java programs to run directly within a web browser. Doing so requires the assistance of a browser plugin, which can lead to compatibility issues between different plugin versions. Browser manufacturers have recently announced that they will no longer include plugins to support applets, and Oracle officially deprecated applets in Java 9.

If the user’s computer does not have the Java runtime installed (or has an incorrect version of the runtime), Java Web Start offers to download and install the Java runtime automatically. As a result, even if the user doesn’t have Java installed, the user can install Java and run the Java application with just a few mouse clicks.

Ordinarily, applications that run via Java Web Start are prevented from performing certain operations that might create security risks for the user, such as reading and writing files on the user’s file system or accessing the user’s local network. You can configure Java Web Start security features to allow these types of activity, but doing so is beyond the scope of this book. For more information, search the web for Java Web Start.

The general process for setting up an application to run with Java Web Start is as follows:

  1. Create a JAR file that contains the Java application you want to run via Java Web Start.

    For information about how to create a JAR file, refer to Book 3, Chapter 8. For the purposes of this chapter, I assume you’ve created a JAR file named ClickMe.jar for the ClickMe application.

  2. Create a JNLP file that specifies the necessary information to enable the program to run from Java Web Start.

    Among other things, the JNLP file must contain the location of the application’s JAR file. For the application illustrated in this chapter, we’ll name the JNLP file clickme.jnlp.

    For more information about creating a JNLP file, see the next section.

  3. Create an HTML file that contains a link to the JNLP file.

    The HTML file will be displayed in the user’s browser. When the user clicks the link to the JNLP file, the application defined in the JNLP file is run. For this chapter, the HTML file will be named clickme.html.

    For more information about creating the HTML file, see the section “Creating an HTML File to Launch a Java Application” later in this chapter.

  4. Copy the JAR, JNLP, and HTML files to the appropriate folders on the web server.

    You can use any FTP client (such as FileZilla) to upload the files to your web server, or you can use your web host’s file management utilities to upload the files. In most cases, the JAR, JNLP, and HTML file will all reside in the same folder on the web server. However, it is possible to save each of the files to a different folder.

Creating a JNLP File

To deploy a Java application via Java Web Start, you must create a JNLP file that contains the details needed to locate and run the application. The JNLP file is a small XML file that contains information such as the name of the application, a URL that points to the application’s JAR file, the vendor who created the application, the Java version the application requires, and a URL that can be used to download the Java runtime in case it isn’t already installed on the user’s computer.

technicalstuff JNLP stands for Java Network Launch Protocol, but that won’t be on the test.

If you aren’t already familiar with the basics of XML, you can learn about XML’s syntax requirements in Book 8, Chapter 5.

Listing 1-2 shows a JNLP file named ClickMe.jnlp that can be used to run the ClickMe application from the website www.lowewriter.com. for the ClickMe application.

LISTING 1-2 The JNLP File for the ClickMe Application

<?xml version="1.0" encoding="UTF-8"?>
<jnlp spec="1.0+"
codebase="http://www.lowewriter.com/ClickMe" →3
href="ClickMe.jnlp"> →4
<information>
<title>ClickMe</title> →6
<vendor>LoweWriter</vendor> →7
<offline-allowed/>
</information>
<resources>
<!-- Application Resources -->
<j2se version="1.6+"
href="http://www.oracle.com/technetwork/java/javase/downloads"/>
<jar href="ClickMe.jar" →14
main="true" />
</resources>
<application-desc main-class="ClickMe"> →17
</application-desc>
<update check="background"/>
</jnlp>

Most of the lines in this JNLP file are written the same for all Java Web Start applications. The lines that you need to change for your particular Java Web Start application are described in the following paragraphs:

  1. →3 The codebase attribute should be changed to reflect the root location where your application is saved on your website. In this case, the application’s files are stored at http://www.lowewriter.com/ClickMe. Although the codebase attribute is not absolutely required, using it can simplify the rest of the JNLP file because URLs that appear elsewhere in the JNLP file are relative to the codebase path. You’ll see what I mean in the next paragraph.
  2. →4 The href attribute indicates the path to the jnlp file. Note that in this example, I specified just the name of the file rather than the complete path. That’s because the path given here is relative to the path given in the codebase attribute. Because the jnlp file is in the http://lowewriter.com/ClickMe folder, specifying just the filename is sufficient here to identify the jnlp file.
  3. →6 The <title> element provides the name of the application — in this case, “ClickMe.”
  4. →7 The <vendor> attribute provides the name of the application’s vendor — in this case, “LoweWriter.”
  5. →14 The href attribute of the jar element provides the location of the application’s JAR file. Again, because this file resides in the folder indicated by the codebase, I specified just the filename here.
  6. →17 The main-class attribute of the application-desc element indicates the name of the application’s main class, which contains the main method that is called to start the application. In this case, the main class is ClickMe.

Creating an HTML File to Launch a Java Application

Once you’ve created a JNLP file that describes your Java Web Start application, you can create an HTML file that your users can view to launch the application. The HTML file should have a link to the JNPL file. The easiest way to provide that link is to use an a element in the HTML file, as shown in the ClickMe.html file shown in Listing 1-3.

LISTING 1-3 An HTML File That Links to a JNPL File

<html>
<head>
<title>ClickMe (Java Web Start)</title>
</head>
<body>
<h1>ClickMe!</h1>
<a href=ClickMe.jnlp>Run the ClickMe application</a>
</body>
</html>

In Listing 1-3, the a element provides the necessary link:

<a href-clickme.jnlp>Run the ClickMe application</a>

Figure 1-2 shows how the ClickMe.html file appears when displayed in a browser window.

image

FIGURE 1-2: The ClickMe HTML page.

Uploading the Java Web Start Files to Your Web Server

For this simple Java Web Start application, I’ve created a total of five files:

  • ClickMe.java: The source file for the ClickMe application, shown in Listing 1-1.
  • ClickMe.class: The compiled class file for the ClickMe application, created by the Java compiler.
  • ClickMe.jar: The JAR file that contains the ClickMe class. This file will be uploaded to the web server, and in turn downloaded to the user’s computer when the user launches the application from the web page.
  • ClickMe.jnlp: The JNLP file that references the ClickMe.jar file. This file will also be uploaded to the web server.
  • ClickMe.html: The HTML file that provides a link to the ClickMe.jnlp file. This file, too, will be uploaded to the web server.

The JAR, JNLP, and HTML files all need to be uploaded to your web server so your users can access them via a browser. You can use whatever means you prefer to upload the files. If you prefer FTP, use an FTP client such as FileZilla to upload the files. If your web host provides an upload tool, you can use it. Or if your web server is local to your own network (for example, if it is an intranet server), you can simply use your operating system to copy the files.

Regardless of how you accomplish it, the goal is to get the JAR, JNLP, and HTML files in a folder on your web server. For the ClickMe application, I uploaded the files to the ClickMe folder under my web root folder, so the HTML file will be accessible via the URL http://www.lowewriter.com/ClickMe/ClickMe.html.

Launching the ClickMe Application Using Java Web Start

When all the files are in place, a web user can access the ClickMe application by opening a web browser and navigating to the URL for the HTML page (in our example, http://www.lowewriter.com/ClickMe/ClickMe.html). Refer to Figure 1-2 to see how the page appears in the browser window.

When the user clicks the Run the ClickMe application link, the dialog box shown in Figure 1-3 appears, asking the user whether she wants to run the application.

image

FIGURE 1-3: The Security Warning prompt displayed when a user clicks a Java Web Start link.

Assuming the user clicks Run, the ClickMe application is then run, as shown in Figure 1-4.

image

FIGURE 1-4: The ClickMe application run from Java Web Start.

Creating an Exception to Allow Java Web Start Applications to Run

In some cases, Java’s security settings may prevent a Java Web Start application from running. If that happens, the user can correct the problem by adding the URL of the application to the Java Exception Site List. Here are the steps for doing that on a Windows computer:

  1. Open the Java Control Panel.

    The easiest way to do that is to press the Start button, type java, press Enter, and then click Configure Java.

    Figure 1-5 shows the Java Control Panel for Java 9 (previous versions are similar).

  2. Click the Web Settings tab.

    Because you’re changing a security setting, you might think you’d need to click the Security tab. But the Exception Site List is found on the Web Settings tab, as shown in Figure 1-6.

  3. Click Add.

    This pops up a text entry box in which you can enter a URL.

  4. Type the URL of the web site you want to add and press Enter.

    In this example, type http://www.lowewriter.com. The URL will be added to the exception list.

  5. Click OK to close the Java Control Panel.

    You should now be able to run the application via Java Web Start.

image

FIGURE 1-5: The Java Control Panel.

image

FIGURE 1-6: The Web Settings tab.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset