The new applet experience

Applet development with JavaFX Script and Java SE 6u10

1 2 3 4 Page 3
Page 3 of 4

How to digitally sign the Weather applet

Unlike appletviewer, which recognizes policy files, Internet browsers require applets to be digitally signed. For the purpose of this article, a self-signed test certificate is sufficient. Carry out the following steps to create a new key and keystore on which the certificate is based, create this certificate, sign Weather.jar, and sign the javafxrt.jar and Scenario.jar files:

  1. Create a new key in a new keystore: keytool -genkey -keystore myKeyStore -alias me

    Alias "me" is arbitrary. It reminds you that the certificate based on the keystore (named myKeyStore) is self-signed so that you don't accidentally put it into production.

    The

    keytool

    program prompts you for information about the new key: It asks you for a password to protect the keystore. Then it asks you for your first and last name, the name of your organizational unit, the name of your organization, the name of your city or locality, the name of your state or province, and the two-letter code of your country. If the values you've entered are correct, respond with

    yes

    to the "

    Is...correct?

    " prompt. Finally, press Enter to assign the keystore password as the password for the key.

  2. Create a self-signed test certificate based on the keystore: keytool -selfcert -alias me -keystore myKeyStore

    Enter the keystore password when prompted.

  3. Sign the Weather applet's JAR file with the testing certificate: jarsigner -keystore myKeyStore Weather.jar me

    Enter the keystore password when prompted.

    The

    jarsigner

    program updates the JAR file's

    META-INF

    directory to contain certificate information and digital signatures for each entry in the archive. If all goes well, you end up with a signed

    Weather.jar

    file.

  4. Sign the JavaFX runtime JAR files with the testing certificate: jarsigner -keystore myKeyStore Scenario.jar me and jarsigner -keystore myKeyStore javafxrt.jar me

    For each command, enter the keystore password when prompted.

Before we explore applet deployment via the Java Deployment Toolkit, I recommend uninstalling any Java Runtime Environments (JREs) for Java SE 6 that are installed on your platform, because the expected behavior shown in the following section will probably differ if one of these JREs is present. You shouldn't need to uninstall any installed Java SE 6 JDKs (with their private JREs).

Applet deployment

The Java Deployment Toolkit includes a deployJava.js JavaScript file whose runApplet(attributes, parameters, minimumversion) function simplifies the task of deploying an applet. Furthermore, this function ensures that an appropriate version of the Java runtime is installed before running the applet. Listing 4 demonstrates runApplet() in the context of the Weather applet.

Listing 4. Weather2default.html

<html>
  <head>
    <script src="http://java.com/js/deployJava.js">
    </script>
  </head>

  <body>
    <script>
      var attributes =
      {
        code: 'javafx.ui.Applet',
        width: 375,
        height: 375,
        archive: 'javafxrt.jar,Scenario.jar,Weather.jar'
      };

      var parameters =
      {
         AppletClass: 'Weather',
         image: 'title.jpg'
      };

      var version = '1.6.0';

      deployJava.runApplet (attributes, parameters, version);
    </script>
  </body>
</html>

The attributes parameter specifies the same <applet> tag attributes that were revealed in Listing 3. Similarly, parameters specifies the same AppletClass parameter, but also specifies an image parameter for replacing the startup Java animation (which is displayed before the applet loads) with a custom image (in title.jpg). Finally, parameter version identifies the minimum Java version needed to run the applet.

To experiment with the default installer, point your browser to Weather2default.html. For example, after pointing Mozilla Firefox 3 to this file, this browser presented the content shown in Figure 2.

In the absence of an installed JRE, runApplet() requires users to manually download Java SE 6 Update 5.
Figure 2. In the absence of a suitable installed JRE, runApplet() requires users to manually download Java SE 6 update 5. (Click to enlarge.)

Click the Download Now button to continue. In response, Firefox presents a message box for obtaining an installer program called jxpiinstall.exe, as Figure 3 reveals.

jxpiinstall.exe takes care of installing Update 5 on Windows platforms.
Figure 3. jxpiinstall.exe takes care of installing Java SE 6 update 5 on Windows platforms.

Click Save File to save jxpiinstall.exe to your desktop. Then double-click the icon that's created on your desktop to begin the installation. Figure 4 presents the initial dialog box.

The initial dialog box requires a button click to view the license agreement.
Figure 4. To avoid intimidating users, the initial dialog box requires a button click to view the license agreement. (Click to enlarge.)

Accept the license agreement by clicking the Accept button. Assuming that you haven't checked the "Show advanced options" panel, this leads you to Figure 5's Java Setup dialog box.

The Java Setup dialog box.
Figure 5. The user must wait until Java SE 6 update 5 finishes installing. (Click to enlarge.)

Click the Finish button on the final Java Setup - Complete dialog box to quit the installer. At this point, update 5 has been completely installed.

Because the browser doesn't automatically start running the applet, you need to once again point the browser to Weather2default.html. This time, you should see the content shown in Figure 6.

Click the Run button to run the applet.
Figure 6. Click the Run button to run the applet. (Click to enlarge.)
1 2 3 4 Page 3
Page 3 of 4