Deploying an Applet with JNLP

Posted by {"name"=>"Palash Ray", "email"=>"paawak@gmail.com", "url"=>"https://www.linkedin.com/in/palash-ray/"} on January 25, 2014 · 3 mins read

JNLP is supposed to be the latest and greatest in the Java Applet world post 1.6 release! Personally, I was never a great fan of Applets. I have always felt that Applets are a pain in the wrong place. Inherently difficult and cumbersome to use and deploy.
But some of these concerns have been addressed with the deployJava javascript API. For example, it is now possible to ask the users to automatically install Java plugin or upgrade to a higher version with no extra effort on part of the developer. Java realised this quite late in the game that its very important to make it as user friendly as possible.
Anyway, enough of ranting :), lets outline the steps one by one.

Creating the Applet

An applet is any class which extends the javax.swing.JApplet. A typical applet is shown below:

public class MyApplet extends JApplet {
    @Override
    public void init() {
	try {
	    SwingUtilities.invokeAndWait(new Runnable() {
		@Override
		public void run() {
		    getContentPane().add(new MyPanel(), BorderLayout.CENTER);
		}
	    });
	} catch (InvocationTargetException | InterruptedException e) {
	    e.printStackTrace();
	}
    }
}

Note that the initialization code which actually adds components to the contentPane should be included in the init() method.

Creating a Jar with proper entries in its Manifest file

Its recommended that we create the following entries in the Jar's Manifest file:
Application-Name: CountDownLatchDemo
Permissions: sandbox
Codebase: palashray.com
The first parameter is the name of the application. The second is the kind of permission that the Applet needs to run. And the third is the domain name from where the Applet will be hosted. Please refer to this for details.
This can either be achieved by manually creating the jar and specifying the manifest file with the m option:

jar -cvmf my_manifest_file.txt myjar.jar

Or it can be done elegantly with Maven. This is the sample pom.xml:

	
		
			
				org.apache.maven.plugins
				maven-jar-plugin
				2.4
				
					
						
							sandbox
							palashray.com
							CountDownLatchDemo
						
					
				
			
		
	

 

Signing the Jar

We need to sign the Jar that has been created above. You can refer to my earlier blog here.

Creating the JNLP file



    
        CountDownLatchDemo
        Palash Ray
    
    
        
        
        
    
    
     
     

Please note that this jnlp file should be accessible by: http://palashray.com/abc/xyz/def.jnlp
And the my.jar and def.jnlp should be in the same directory.

Use the JNLP file in a HTML file to display the Applet

	

Since we are invoking the applet through the deployJava API, the user will be prompted for installing or upgrading the java plugin in the browser.
For more information, please refer to this url.