Mar 17, 2012

Java Applets in HTML5

Java Applets in HTML5

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



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



3. 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 and Sample.jar.pack.gz
::] pack200 -r Sample.jar

::] pack200 Sample.jar.pack.gz Sample.jar

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. (deflated about 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 much smaller (deflated about 70% 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



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


https://eyeasme.com/Shayne/HTML5_APPLETS/

No comments:

Post a Comment