Tutorial: Building an 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:
GoodEveningApplet.class
must be in a firstapplet
directory to reflect the package structure: firstapplet/GoodEveningApplet.class
. If you're deploying to a JAR file, check the directory structure in the file and make sure it matches.
CODEBASE
attribute.
"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:
GoodEveningApplet.html
has the fully qualified class name, including the package name: firstapplet.GoodEveningApplet
.
CODEBASE
attribute in GoodEveningApplet.html
specifies the correct location of the class file relative to the HTML file. In this example, the CODEBASE
is ".", because the JAR file containing the class file will be in the same directory as the HTML file. If the class files were located in a different directory, such as a class
directory, the value for the CODEBASE
directory would be as follows,
CODEBASE = "classes"
.
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:
For example,jar [ options ] [manifest] destination input-file [input-files]
This command creates a JAR file of all the classes in the current directory.jar cf myjarfile.jar *.class
To include all files in the directory, use this command:
jar cf myjarfile.jar *
jar -help
at the command line.
Create the JAR file in the following steps:
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.
classes
directory in your FirstApplet
project: /[home]/jbproject/FirstApplet/classes
.
The firstapplet
directory should be in this directory.
jar cf GoodEvening.jar *
/jbuilder/jdk1.3/bin/jar cf GoodEvening.jar *
classes
directory. Open the JAR file and check that the directory structure is correct: firstapplet/GoodEveningApplet.class
.
GoodEvening.jar
to the applets
directory for testing.
After creating the JAR file, continue to Step 8: Modifying the HTML file.
"Using JAR Files: The Basics" at http://java.sun.com/docs/books/tutorial/jar/basics/index.html
"jar-The Java Archive Tool"
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:
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.
Applet
in the Name field.
applets
directory you created in the /[home]/jbproject
directory. Change the JAR file name to GoodEvening.jar
.
Applet
appears in the project pane. You can modify this file by right-clicking and selecting Properties.
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.
Tutorial: Building an applet
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,
GoodEveningApplet.html
in JBuilder and add the ARCHIVE
attribute:
APPLET
tag:
TheARCHIVE = "GoodEvening.jar"
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>
ARCHIVE="file1.jar, file2.jar"
ARCHIVE
tag.
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.
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.
GoodEveningApplet.html
from the project's src
directory to the applets
directory. The applets
directory should contain two files, GoodEveningApplet.html
and GoodEvening.jar
.
src
directory. Do not copy FirstApplet.html
to the applets
directory or your applet will not run.
Tutorial: Building an applet
To run the applet at the command line,
GoodEveningApplet.html
and GoodEvening.jar
are in the applets
directory.
set CLASSPATH=
unsetenv CLASSPATH
unset CLASSPATH
applets
directory.
/jbuilder/jdk1.3/bin/appletviewer GoodEveningApplet.html
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:
Tutorial: Building an applet
Complete these steps, then test your applet on the Web.
CODEBASE
attribute in the HTML file and that the CODE
attribute has the fully qualified class name (including the package name).
To open the Java Console:
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: