A browser with JavaScript enabled is required for this page to operate properly.
Trail: Deployment
Lesson: Deployment In-Depth
Section: Deployment Toolkit
Subsection: Deploying an Applet
Embedding JNLP File in Applet Tag
Home Page > Deployment > Deployment In-Depth

Embedding JNLP File in Applet Tag

When applets are deployed by using the Java Network Launch Protocol (JNLP), the Java Plug-in software launches the applet after downloading the JNLP file from the network. Beginning in the Java SE 7 release, you can reduce the the time it takes for applets to launch, by embedding the JNLP file in the web page itself so that an additional network request can be avoided the first time the applet is loaded. This will result in applets launching quickly on the web browser.

A Base64 encoded JNLP file can be embedded in the jnlp_embedded parameter when deploying an applet in a web page. The attributes of the <jnlp> element should meet the following restrictions:

The following steps describe how to embed a JNLP file in a web page to deploy an applet.

  1. Create a JNLP file for your applet. A sample file is shown next.
    <?xml version="1.0" encoding="UTF-8"?>
    <!-- href attribute contains relative path;
         codebase attribute not specified -->
    <jnlp href="dynamictree-applet.jnlp">
        <information>
            <title>Dynamic Tree Demo</title>
            <vendor>Dynamic Team</vendor>
        </information>
        <resources>
            <!-- Application Resources -->
            <j2se version="1.6+" />
            <jar href="dist/applet_ComponentArch_DynamicTreeDemo/DynamicTreeDemo.jar" 
                 main="true" />
        </resources>
        <applet-desc 
             name="Dynamic Tree Demo Applet"
             main-class="appletComponentArch.DynamicTreeApplet"
             width="300"
             height="300">
         </applet-desc>
         <update check="background"/>
    </jnlp>
    
  2. Encode the contents of the JNLP file using the Base64 scheme. You can use any Base64 encoding tool to encode the the JNLP file. Check the usage of the tool to create a string with Base64 encoding. Some examples of tools and web sites that may be used are as follows:
  3. When deploying the applet in a web page, specify the jnlp_embedded parameter with it's value set to the Base64 encoded JNLP string. Make sure to include only the actual Base64 bytes without any encoding tool specific headers or footers.
    <script src="http://www.java.com/js/deployJava.js"></script>
    <script>
        var attributes = {} ;
        <!-- Base64 encoded string truncated below for readability -->
        var parameters = {jnlp_href: 'dynamictree-applet.jnlp',
            jnlp_embedded: 'PCEtLSAKLyoKICogQ29weX ... HA+Cg=='
        } ;
        deployJava.runApplet(attributes, parameters, '1.6');
    </script>
    

    Some encoding tools may wrap the encoded string into several 76-column lines. To use this multi-line attribute value in JavaScript code, specify the attribute value as a set of concatenated strings. You can include the multi-line attribute value as is if the applet is deployed directly with the <applet> HTML tag.

Open AppletPage.html in a browser to view the Dynamic Tree Demo applet that is launched by using the JNLP file embedded in the web page.


Note: If you don't see the applet running, you need to install at least the Java SE Development Kit (JDK) 7 release.

Note: If you don't see the example running, you might need to enable the JavaScript interpreter in your browser so that the Deployment Toolkit script can function properly.

Download source code for the Embedded JNLP example to experiment further.


Problems with the examples? Try Compiling and Running the Examples: FAQs.
Complaints? Compliments? Suggestions? Give us your feedback.

Previous page: Customizing the Loading Screen
Next page: Deploying a Java Web Start Application