Java Applets in HTML5

A Sample Applet

Example of an applet that runs class Sample located in file Sample.jar
NOTE: Need Java 6 (or later version).

Applet failed to run. No Java plug-in was found.

Creating the Sample Applet

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%)

Code to Add Sample Applet to HTML Page

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>

A Better Way

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 ).

Creating the Compressed jar File 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

Why Use 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.

Finally, the Best Way to Add A Java Applet to An HTML5 Document

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>
Copyright © 2011 Shayne Steele  
Comments / Suggestions / Corrections to : (shayne.steele <AT> eyeasme <DOT> com)
This document is licensed under the GFDLv1.3, All software shown is licensed under the LGPLv3.
Document last modified on August 09, 2011 09:06 pm MST.