Example of an applet that runs class Sample
located in file Sample.jar
NOTE: Need Java 6 (or later version).
Create Sample.jar from Sample.java, like so:
::] javac -version javac 1.6.0_21 ::] javac Sample.java ::] jar cvf Sample.jar *.class added manifest adding: Sample$1.class(in = 1087) (out= 607)(deflated 44%) adding: Sample$2.class(in = 1075) (out= 612)(deflated 43%) adding: Sample$3.class(in = 1259) (out= 739)(deflated 41%) adding: Sample$4.class(in = 900) (out= 547)(deflated 39%) adding: Sample$5.class(in = 1099) (out= 670)(deflated 39%) adding: Sample.class(in = 4193) (out= 2190)(deflated 47%)
OLD WAY: Java applet in HTML 4.01
<applet code="Sample" archive="Sample.jar" height="300" width="550"> Applet failed to run. No Java plug-in was found. </applet>
NEW WAY: Java Applet in HTML5 (uses object
tag)
<object type="application/x-java-applet" height="300" width="550"> <param name="code" value="Sample" /> <param name="archive" value="Sample.jar" /> Applet failed to run. No Java plug-in was found. </object>
pack200
jar Compression
There is a way to GREATLY reduce the save of the downloaded jar file;
that way is to use
pack200
(
pack200 and Compression
).
Sample.jar.pack.gz
NOTE: wc -c
gives the size in bytes of a file
::] wc -c Sample.jar 6426 Sample.jar ::] pack200 -r Sample.jar ::] wc -c Sample.jar 6337 Sample.jar ::] pack200 Sample.jar.pack.gz Sample.jar ::] wc -c Sample.jar.pack.gz 2113 Sample.jar.pack.gz
pack200
and How to Use It in An Applet
NOTE: The command: pack200 -r Sample.jar
,
repacks the Sample.jar
file
(This produces a slightly smaller jar file. Old size was 6426 bytes; new size is 6337 bytes. (deflated 1%))
NOTE: The command: pack200 Sample.jar.pack.gz Sample.jar
,
produces a compressed jar file named Sample.jar.pack.gz
(Size of Sample.jar.pack.gz
is 2113 bytes
(deflated 67% from original Sample.jar
))
When the following line is added to the Java applet:
<param name="java_arguments" value="-Djnlp.packEnabled=true"/>
the Java plug-in will automatically download and try using the compressed jar file
(Sample.jar.pack.gz
).
If the browser is unable to use the compressed jar file, the uncompressed jar file
(Sample.jar
) will be downloaded.
BEST WAY: Java Applet in HTML5 (using pack200
jar compression)
<object type="application/x-java-applet" height="300" width="550"> <param name="code" value="Sample" /> <param name="archive" value="Sample.jar" /> <param name="java_arguments" value="-Djnlp.packEnabled=true"/> Applet failed to run. No Java plug-in was found. </object>