<applet code="classname.class" WIDTH = 400 HEIGHT = 300>When the browser reads this line, it makes a connection to the Web server and fetches the file. The class loader of the Java interpreter that is built into the browser then loads the specified class from that file. During the loading process, the class loader must resolve the other classes used in the class.
After doing so (if an applet that uses multiple classes), it then knows it needs more classes to run the applet. The browser, then must make multiple (possibly many more) connections to the Web server, one for each class file. Even for a small applet, this can be a very time consuming process. Loading an applet that takes dozens of classes can take many minutes.
Java supports an improved method for loading class files, which allows you to package all the needed class files into a single file.
JAR stands for Java ARchive. It's a file format based on the popular ZIP file format and is used for aggregating many files into one. Although JAR can be used as a general archiving tool, the primary motivation for its development was so that Java applets and their requisite components (.class files, images and sounds) can be downloaded to a browser in a single HTTP transaction, rather than opening a new connection for each piece. This greatly improves the speed with which an applet can be loaded onto a web page and begin functioning. The JAR format also supports compression, which reduces the size of the file and improves download time still further. Additionally, individual entries in a JAR file may be digitally signed by the applet author to authenticate their origin.
JAR is:
JAR consists of a zip archive, as defined by PKWARE, containing a manifest file and potentially signature files, as defined in the Manifest and Signature specification. (see below)
Synopsis
jar c|t|x[f][m][v] [jar-file] [manifest-file] [files]
Examples
Here is a page with the jar options
Once you have created a JAR file, you refer to it in the <APPLET> tag with the archive attribute.
<applet code=Animator.class
archive="jars/animator.jar"
width=460 height=160>
<param name=foo value="bar">
</applet>
Note that the familiar CODE=myApplet.class parameter must still be present.
The CODE parameter, as always, identifies the name of the applet where execution
begins. However, the class file for the applet and all of its helper classes are loaded
from the JAR file.
Once the archive file is identified, it is downloaded and separated into its components. During the execution of the applet, when a new class, image or audio clip is requested by the applet, it is searched for first in the archives associated with the applet. If the file is not found amongst the archives that were downloaded, it is searched for on the applet's server, relative to the CODEBASE (that is, it is searched for as in JDK1.0.2).
The archive tag may specify multiple JAR files. Each JAR file must be separated by "," (comma). Each file is downloaded in turn:
<applet code=Animator.class
archive="classes.jar , images.jar , sounds.jar"
width=460 height=160>
<param name=foo value="bar">
</applet>
There can be any amount of white space between entries within the archive parameter.
In addition, the archive tag itself is case-insensitive; it can be lower-case,
upper-case, or any combination of lower- and upper-case letters, such as ArCHiVe.
Access files in jars from command line/executable files
See the tools docs to see how to call .jar files from the command line. Specifically...
Here are the java command options (note the use of -jar) which mentions the use
of the JAR Manifest for using -jar. If you do not like reading the formal documentation, here is a page I made to show you how to make an executable jar
Or, you can just look at the jar tutorial section on this topic.
To prepare a Bean for a Beanbox, you must package it up in a JAR file along wiht any other classes or resources it requires. The manifest of the JAR must define which of the JAR file entries are beans.
When you use the m option in conjunction with c, it tells jar to read a partial manifest file that you specify. This file, perhaps named manifest.stub should have contents:
(Note Nutshell, page 183 for directory information)
Each bean must be specified as such in the manifest.
Your minimal Bean will be named SimpleBean.
Here are the steps to create it and view it in the BeanBox:
SimpleBean.java, in the directory
of your choice. Here's the code:
import java.awt.*;
import java.io.Serializable;
public class SimpleBean extends Canvas
implements Serializable{
//Constructor sets inherited properties
public SimpleBean(){
setSize(60,40);
setBackground(Color.red);
}
}
SimpleBean extends the
java.awt.Canvas
component. SimpleBean also implements the
java.io.Serializable interface,
a requirement for all Beans. Setting the background
color and component size is all that SimpleBean does.
CLASSPATH
environment variable is set to point to all
needed .class (or .jar) files.
javac SimpleBean.java
This produces the class file SimpleBean.class
manifest.tmp),
that contains the following text:
Name: SimpleBean.class
Java-Bean: True
SimpleBean class file:
jar cfm SimpleBean.jar manifest.tmp SimpleBean.class
The Java Sun website contains documentation on
JAR files.
SimpleBean.jar location and
select it. SimpleBean will appear at the bottom
of the ToolBox. (Note that when the BeanBox is
started, all Beans in JAR files in the
beans/jars directory are automatically
loaded into the ToolBox).
SimpleBean instance into the BeanBox.
Click on the word SimpleBean in the ToolBox. The cursor
will change to crosshairs. Move the cursor to a spot within the
BeanBox and click. SimpleBean will appear as a
painted rectangle with hatched border. This border means
that SimpleBean is selected. The
SimpleBean properties will appear in the
Properties sheet.