Overview Step 6 Step 8Source code

Tutorial: Building an applet

Step 7: Deploying your applet

Deploying a Java applet consists of bundling together the various Java class files, image files, and other files needed by your applet and copying them and the applet HTML file to a location on a server or client computer where they can be executed. You can deliver the files separately, or you can deliver them in compressed or uncompressed archive files. JAR files, Java archive files, are the most commonly used. JAR files provide the advantages of smaller file sizes and faster download times.

When deploying your applet, it's important to remember the following:

See also:
"Deploying Java programs" in Building Applications with JBuilder.
Step 16 of the JBuilder tutorial "Building a Java text editor," Deploying the Text Editor application

Depending on the edition of JBuilder you have, there are several tools for deploying your applet:

Important: Before creating the JAR file, review this checklist:

Deploying your applet with the jar tool

JBuilder Foundation

The JDK includes a jar tool in the bin directory for creating JAR files for deployment. The jar tool, an archiving and compression tool, combines multiple files into a single JAR archive.

The basic jar command is in the following form:

jar [ options ] [manifest] destination input-file [input-files]
For example,

jar cf myjarfile.jar *.class
This command creates a JAR file of all the classes in the current directory.

To include all files in the directory, use this command:

jar cf myjarfile.jar *

Note: For help on additional JAR options, enter jar -help at the command line.

Create the JAR file in the following steps:

  1. Save and compile your project.

  2. Create an applets directory for your applet in your /[home]/jbproject directory. This will be the testing directory where you'll put your applet HTML file and your JAR file.

  3. Open the command-line shell or DOS window.

  4. Change to the classes directory in your FirstApplet project: /[home]/jbproject/FirstApplet/classes. The firstapplet directory should be in this directory.

  5. Enter the JAR command.
    jar cf GoodEvening.jar *
    Important: The JDK must be on your path. If it isn't, use the following command. If JBuilder is on another drive, include the drive name.
    /jbuilder/jdk1.3/bin/jar cf GoodEvening.jar *

    Note: For Windows, use backslashes (\).

  6. The JAR file is created in the current classes directory. Open the JAR file and check that the directory structure is correct: firstapplet/GoodEveningApplet.class.

  7. Copy GoodEvening.jar to the applets directory for testing.

After creating the JAR file, continue to Step 8: Modifying the HTML file.

See also:
"Using JAR Files: The Basics" at http://java.sun.com/docs/books/tutorial/jar/basics/index.html
"jar-The Java Archive Tool"

Deploying your applet with the Archive Builder

This is a feature of JBuilder Professional and Enterprise.
JBuilder's Archive Builder collects all the files needed to distribute your applet and can archive them into a JAR file.

To deploy your applet with JBuilder Professional and Enterprise:

  1. Save and compile your project.

  2. Create an applets directory for your applet in your /[home]/jbproject directory. This will be the testing directory where you'll put your applet HTML file and your JAR file.

  3. Choose Wizards|Archive Builder to open the Archive Builder.

    Archive Builder Step 1

  4. Select Applet from the Archive Type drop-down list in Step 1. Click Next to continue to the Step 2.

  5. Accept the default name of Applet in the Name field.

  6. Click the ellipsis button next to the Path field and browse to the applets directory you created in the /[home]/jbproject directory. Change the JAR file name to GoodEvening.jar.

    Archive Builder Step 2

  7. Accept the default settings in Steps 3, 4, and 5. Note that in Step 5 the option is set to create a manifest file for the archive.

    Note: For information on manifest files, see "About the manifest file" in "Deploying Java programs."

  8. Click Finish to exit the Archive Builder. An archive node called Applet appears in the project pane. You can modify this file by right-clicking and selecting Properties.

  9. Compile your project by selecting Project|Make Project. The Archive Builder gathers all the files in the project's output path (Project|Project Properties|Paths) into the JAR file.

  10. Expand the  icon next to the Applet archive node to see the GoodEvening.jar archive file. Double-click the JAR file in the project pane. The manifest file appears in the content pane and the contents of the JAR file appear in the structure pane. Select a file in the structure pane to view it in the content pane.

Note: If you are delivering multiple programs to the same location, you can deliver the redistributable files separately, rather than include them in each of your JAR files.


Overview Step 7 Step 9

Tutorial: Building an applet

Step 8: Modifying the HTML file

Now that your applet is deployed in a JAR file, you need to modify the HTML file with the ARCHIVE attribute and include the JAR file name. We'll also add a message inside the APPLET tags that tells users without Java-enabled browsers that they won't be able to see the applet unless they enable Java in their browser or upgrade their browser.

To modify the HTML file,

  1. Open GoodEveningApplet.html in JBuilder and add the ARCHIVE attribute:

    1. Select the Source tab to view the HTML source code.

    2. Add the following HTML code inside the APPLET tag:
      ARCHIVE  = "GoodEvening.jar"
      The APPLET tag should look like this:
      <APPLET
        CODEBASE = "."
        CODE     = "firstapplet.GoodEveningApplet.class"
        ARCHIVE  = "GoodEvening.jar"
        NAME     = "TestApplet"
        WIDTH    = 400
        HEIGHT   = 300
        HSPACE   = 0
        VSPACE   = 0
        ALIGN    = middle
      >
      </APPLET>
      

    Tip: If you have multiple JAR files for your applet, list them separated by a comma as shown here:

    ARCHIVE="file1.jar, file2.jar"

    Important: Some older browsers do not support JAR files and multiple listings of archive files but do support a single ZIP file in the ARCHIVE tag.

  2. Next, let's add a message that tells users without Java-enabled browsers that their browsers do not support Java; therefore, they can't see the applet. Enter the following message between the open and close APPLET tags:
    You need a Java-enabled browser to view this applet.
    
    The APPLET tag looks like this:

    <APPLET
      CODEBASE = "."
      CODE     = "firstapplet.GoodEveningApplet.class"
      ARCHIVE  = "GoodEvening.jar"
      NAME     = "TestApplet"
      WIDTH    = 400
      HEIGHT   = 300
      HSPACE   = 0
      VSPACE   = 0
      ALIGN    = middle
    >
    You need a Java-enabled browser to view this applet.
    </APPLET>
    

    Any browser that does not support Java ignores the APPLET tags and displays everything between the tags. Because a Java-enabled browser recognizes the APPLET tags, anyone with a Java-enabled browser will see the applet and not the message.

    Important: Before saving the HTML file, check the CODEBASE and CODE values again. If these values are incorrect, the applet won't run. Remember that the CODEBASE value is the location of the applet code (class or JAR file) in relation to the HTML file. The value, ".", means the class file is in the same directory as the HTML file. The CODE value must be the fully qualified class name for the applet, including the package name.

  3. Save and close the file.

  4. Copy the modified GoodEveningApplet.html from the project's src directory to the applets directory. The applets directory should contain two files, GoodEveningApplet.html and GoodEvening.jar.

    Caution: Remember, JBuilder creates two HTML files: the project notes HTML file located at the root of the project directory and the applet HTML file located in the project src directory. Do not copy FirstApplet.html to the applets directory or your applet will not run.


Overview Step 8 Step 10

Tutorial: Building an applet

Step 9: Running your deployed applet from the command line

It's a good idea to test the deployed applet locally before testing it on the Web. You can do this from the command line using Sun's appletviewer. This will tell you if the browser has everything it needs to run the applet. If any files are missing or if there are any errors in the HTML file, the applet won't run. You can then correct the errors before posting it on the Web.

To run the applet at the command line,

  1. Be sure a copy of GoodEveningApplet.html and GoodEvening.jar are in the applets directory.

  2. Open the command-line window.

  3. Clear any CLASSPATH variables to remove any class path settings for this session as follows:

  4. Change to the applets directory.

  5. Run the appletviewer by entering the following command:

    /jbuilder/jdk1.3/bin/appletviewer GoodEveningApplet.html

    Important: If JBuilder is on another drive, include the drive letter.

    Note: For Windows, use backslashes (\).

  6. If the "Good Evening" applet loads and runs, the deployment was successful and all the classes were found and included. If the applet doesn't run, check the error messages, correct them, recompile, deploy, and test again.

The "Good Evening" applet is available as a sample in the samples/Tutorials/FirstApplet directory of your JBuilder installation.

If you are having problems running your applet, check the applet source code at the end of the tutorial and see these topics for common errors:


Overview Step 9Source code

Tutorial: Building an applet

Step 10: Testing your deployed applet on the Web

The final step in testing your applet is to run it on the Web. This will tell you if it really has all the files it needs.

Complete these steps, then test your applet on the Web.

  1. FTP (file transfer protocol) the applet's HTML and JAR files to an Internet server or copy to a Windows NT server.

    1. Use an FTP utility to transfer the files.

    2. Transfer archives and class files as binary files.

    3. Be sure the HTML and JAR file locations match the CODEBASE attribute in the HTML file and that the CODE attribute has the fully qualified class name (including the package name).

  2. Test the applet in various browsers. If the applet fails to load, check that the browser is Java-enabled. Also check the browser's Java Console for error messages.

    To open the Java Console:

    1. Select Communicator|Tools|Java Console in Netscape.

    2. Select View|Java Console in Internet Explorer.

Congratulations!! You've created your first applet with JBuilder. Now that you're familiar with JBuilder's development environment, you'll find its many time-saving features make your programming easier.

For additional suggestions on improving this tutorial, send email to jpgpubs@inprise.com.

For other applet tutorials, see:

Overview Step 9 Source code