Java 6 Update 10, released in October 2008, contains significant changes for running Java applets. Many of these new features can be utilized without any changes to an applet's code, they just require changes to the deployment of the applet, but some do require code changes.
These new features are currently available in Firefox 3, Chrome and Internet Explorer 6 & 7, on Windows or Linux - there is no OS X support yet.
New features in Java 6 Update 10:
- Speed: Smaller, incremental JRE download and faster applet startup.
- Stability: Applets run in a separate process to the browser, and each applet can optionally run in a separate JVM instance, so one misbehaving applet cannot affect other applets or the browser.
- Configuration: Applets can have parameters passed to them to control behaviour, such as setting a large initial heap size to avoid out of memory exceptions.
- Draggable applets: Applets can now be dragged out of the web browser and run as separate applications.
In this blog entry we'll discuss the changes made to the applet and its deployment for Java 6 Update 10, and from that you can see how to deploy the PDF applet in your own organisation to take advantage of these features.
1 - Draggable applets.
If you activate this feature then users can use ALT-Click to drag the applet out of the browser. (Note: On Ubuntu ALT-Click is used to move windows by default, so you need to change this by opening System-Preferences-Windows and selecting an alternative movement key mapping).
To activate this functionality you just need to add the following parameter to your APPLET tag:
<param name="draggable" value="true"/>
Users can then drag the PDF Viewer applet out of the browser, as shown below.
This will enable the dragging functionality, but by default the applet will not look like a standard application when it is dragged, to do that you need to implement the following methods in your applet class:
public void appletDragStarted(); public void appletDragFinished(); public void setAppletCloseListener(ActionListener l) ; public void appletRestored();
These methods allow you to put the applet in a standard window frame and do any other necessary changes when the applet is dragged from the browser. See http://www.oracle.com/technetwork/articles/javase/index-137083.html for more details on these methods.
You should also provide a JNLP file in order for this to work, as once the applet is dragged out of the browser it uses the Java WebStart functionality, which relies on the JNLP file.
2 - Separate JVM
To specify that your applet should run in its own JVM instance then you can use the 'separate_jvm' APPLET parameter:
<param name="separate_jvm" value="true"/>
Once you have added these features to your APPLET tag it should look something like this:
<applet code="org.faceless.pdf2.viewer2.PDFViewerApplet" name="pdfapplet" codebase="applet" archive="bfopdf.jar" style="width:100%; height:500px; display:block"/> <param name="feature.MultiWindow" value="false" /> <param name="feature.Toolbars" value="false" /> <param name="feature.Menus" value="false" /> <param name="liveconnect.DocumentPanelListener" value="documentUpdated" /> <!-- Identify a JNLP file to be used by--> <!-- Java 6 Update 10 and above --> <param name="jnlp_href" value="bfopdf-applet.jnlp"/> <!-- Request a draggable applet --> <param name="draggable" value="true"/> <!-- Request a separate JVM instance --> <param name="separate_jvm" value="true"/> <!-- The next three parameters control the --> <!-- logo displayed when the applet is loading --> <param name="image" value="http://bfo.com/external/loading.gif"/> <param name="boxborder" value="false"> <param name="centerimage" value="true"> </applet>
You can easily remove any of the features that you do not wish to be available (e.g. draggable applets).
To complete the deployment we need to supply a JNLP file for the applet, which we reference as a parameter from the APPLET tag, such as:
<param name="jnlp_href" value="bfopdf-applet.jnlp"/>
The use of JNLP files for applet deployment is one of the main changes in Update 10. Once the JVM sees the 'jnlp_href' parameter then it will pick up details about the Applet JAR file, main class, widht and height from the JNLP file, ignoring anything set in the APPLET tag.
This means that you can use the APPLET details to deploy the applet to clients running older JRE versions, and the JNLP file to deploy the applet to customers on newer JREs. This enables you to run different code, by deploying different JARs, for pre or post Update 10 JREs.
Full details of the JNLP specification for applets can be found here: http://www.oracle.com/technetwork/java/javase/jnlp-136707.html, but we will discuss the features enabled in the PDF Viewer Applet.
3 - Desktop shortcut:
When an applet is dragged outside a browser and the user closes the browser window, the applet is then running as a Java WebStart application from the JNLP file, and it can optionally place a WebStart link on the user's desktop so that they can easily launch the WebStart applet again. The entries in the JNLP 'information' section below enable this, and also specify a title and custom icon to use for the shortcut:
<information> <title>BFO PDF Viewer</title> ..... <shortcut online="false"> <desktop /> </shortcut> <icon href="http://bfo.com/external/pdfviewerdesktopicon.gif"/>
Due to a bug in Java the desktop icon used for a dragged applet in Vista and Ubuntu is only 32 x 32, scaled up to 48 x 48, so the desktop icon doesn't look quite as smooth as it could, but it is adequate, as shown below.
4 - Increased heap size:
JNLP files allow you to specify particular JRE versions that your applet is compatible with. Along with a version you can also set the initial and maximum heap sizes to use for that JRE version. In the example below we state that we are compatible with any JRE 1.4 or later, and require a 256MB heap size.
<resources> ..... <!-- Specify that we need Java 1.4 or above, --> <!-- and want 256MB heap size --> <j2se version="1.4+" maximum-heap-size="256M"/>
You can also specify an initial heap size, but we have not found a way of using this and the maximum heap size without an error occurring, so we just use the maximum heap size.
If you put those new features together with the standard JNLP file contents and use an <applet-desc> element, instead of the usual <application-desc>, to provide the applet's size and main class then you should have a JNLP file that looks something like this:
<jnlp spec="1.0+" codebase="http://bfo.com/products/pdf/applet" href="pdfapplet.jnlp"> <information> <title>BFO PDF Viewer</title> <vendor>Big Faceless Organization</vendor> <homepage>http://bfo.com/products/pdf</homepage> <offline-allowed/> <description kind="short"> The BFO PDF Viewer provides a cross-platform applet and application to allow viewing and printing of PDF documents. </description> <!--Create a shortcut on the desktop if--> <!--the user drags the applet out of the browser--> <shortcut online="false"> <desktop /> </shortcut> <icon href="http://bfo.com/external/pdfviewerdesktopicon.gif"/> </information> <security> <all-permissions/> </security> <resources> <!-- Get Pack200 version if available --> <property name="jnlp.packEnabled" value="true"/> <!-- Specify that we need Java 1.4 or above, --> <!-- and want 256MB heap size --> <j2se version="1.4+" maximum-heap-size="256M"/> <!-- The jar our applet is contained in --> <jar href="bfopdf.jar" main="true"/> <!-- Other required JARs, loaded on demand --> <jar href="bfopdf-cmap.jar" part="cmap" download="lazy"/> <jar href="bfopdf-qrcode.jar" part="qrcode" download="lazy"/> <jar href="bfopdf-stamp.jar" part="stamp" download="lazy"/> <package name="org.faceless.pdf2.resources.encodings.*" part="cmap" recursive="true"/> <package name="org.faceless.pdf2.resources.qrcode.*" part="qrcode" recursive="true"/> <package name="org.faceless.pdf2.resources.canvases.stamp" part="stamp" recursive="true"/> </resources> <applet-desc name="PDF Viewer" main-class="org.faceless.pdf2.viewer2.PDFViewerApplet" width="600" height="500"> </applet-desc> </jnlp>
The new features and improvements in Java 6 Update 10 make applets better for end-users to use and easier for developers to deploy. If you haven't already done so then it really is worth upgrading to the latest version, which you can download from www.java.com.