Few days ago I published (here) an applet to encrypt/decrypt Caesar’s cipher. An issue I faced was that an applet must be signed to access windows clipboard. However, a page with a singed applet welcomes you with a security warning! which I so want to avoid on my site. I think that visitors to my blog should not be asked to deal with security messages from their system. The problem now is that users can not copy/paste text from and to the applet unless they run it on their local machines. As such, I thought the applet is functional and can be tested online and if any user require to copy/paste text (s)he may download the applet’s jar file and execute it locally whenever they want. Running the applet on your local machine does not include security restrictions on the code.

To create an executable jar file there should be a main class (a class with a main method) inside the jar to run the software. An applet has no main method since it is usually designed to work inside a web browser. To make the jar file executable I had to do two things:

  • One

I created a new class inside the jar file as a main class which I called StartClass. The job of this class is to call and host the applet; hence if the jar file is executed, the applet method is invoked. Check out the following code that I used supported by few comments to explain what it does.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package CaesarCodePackage;
 
public class StartClass {
 
     public static void main(String [] args)
      {
         // create an object of type CaesarCode which is the main applet class
         CaesarCode theApplet = new CaesarCode();
         theApplet.init();   // invoke the applet's init() method
         theApplet.start();  // starts the applet
 
         // Create a window (JFrame) and make applet the content pane.
          javax.swing.JFrame window = new javax.swing.JFrame("Caesar's Cipher");
          window.setContentPane(theApplet);
          window.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
          window.pack();              // Arrange the components.
          window.setVisible(true);    // Make the window visible.
        }
 
}
  • Two

I had to edit the MANIFEST.MF file and add the following line to define my main class correctly

Main-Class: CaesarCodePackage.StartClass

please note that ‘CaesarCodePackage’ is my package’s name and ‘StartClass’ is the name of my main class (the one with the main method that I used to call the applet). If this step is not performed correctly the following error is more likely to occur if you try to execute the jar file:

Failed to load Main-Class manifest attribute

A final note would be that, to successfully execute jar files on your system, you need to have JRE installed and the Jar files must be associated with javax.exe (javax.exe is located inside the bin directory of your JRE folder e.g. C:\Program Files\Java\jre6\bin)

2 Responses to “Creating an executable jar file for an applet”

Leave a Reply

*

Haider’s WebSpace
Welcome to my technical blog. This is where I write, archive and share computer related articles. Subjects vary from posting technical solutions to researching particular topics. Feel free to comment and talk IT!
Sponsored Links
My Tweets
Posts Calendar
May 2012
M T W T F S S
« Mar    
 123456
78910111213
14151617181920
21222324252627
28293031