Home Page for Applet2App
Author: Raja Ranjan Senapati
Applet2App is a Java Application which
can be used to convert an applet to a Java executable application. Typically, an
applet requires a browser like Internet explorer, Netscape Navigator to
execute. Unless it’s specifically coded, an applet can not execute as a normal
Java application.
Applet2App
adds the necessary wrapper files to the applet class files and creates a
standalone Java 2 compliant executable jar file.
Applet2App is targeted towards applet
developers who also want to distribute their applets as standalone
applications. Once the applet developer is ready with his applet, he can use
Applet2App to create a Java 2 compliant jar file. One INI file will also be
generated. This will typically have the applet attributes like the height,
width etc. The applet developer can also add additional applet specific
parameters to this INI file. These parameters are provided in the Applet tag of
the HTML page which has the reference to the applet. The applet developer has
the option of modifying the INI file directly or through Applet2App
application. Then he can distribute the INI as well as the executable Jar file
to the end user.
Applet2App is a “free
open source” software. You can modify/customize the source code as per your
requirement. But I would appreciate it if you can send me a mail giving me some
feedback. Also you can suggest any modifications/wish list etc.
Applet2App requires JDK1.2 or higher to
execute. It invokes jar utility which comes with JDK to create the executable
Jar files. Since the concept of executable Jar file came with Java 2, so we can
only use JDK 1.2 or higher for Applet2App to execute properly.
For end users, if they have JRE 2
installed on their machine, it should be enough to run the generated executable
jar file. They need not have JDK installed on their machine.
Using Applet2App is really easy and intuitive.
However, to ensure that we don’t add any extra files to the final executable,
let’s do this with an example. 5 years back, I had developed an elementary game
called bricks in Java as part
of learning java. It has following structure.
File Name |
File Type |
bricks.java |
Main applet java file |
bricks_struct.java |
Ancillary file required by
bricks. java |
Demoframe.java |
Class file to create a Help
frame |
ball.gif |
Static resource file
required by the applet |
bricks.html |
HTML file which contains
the applet |
The Java files are stored in a package called myapp.game which means You need to
store the java files in a subfolder structure like myapp/game
in a root folder. Assuming you are working in windows environment and your root
folder is c:\source, the bricks java files are stored in c:\source\myapp\game
and ball.gif and bricks.html are stored in c:\source.
First you need to compile the program.
C:\source\>
javac myapp/game/bricks.java
Note: You
will get some deprecated method warnings, the reason being I have not changed
the code since last 5 years J
Once all the class files are generated,
you can verify whether this piece of code is working or not by clicking on
c:\source\bricks.html
The
contents of Bricks.html are as follows.
<HTML>
<BODY>
<CENTER>
<P>
<APPLET code="myapp.game.bricks"
width=640 height=392></APPLET>
</P>
</CENTER>
</BODY>
</HTML>
Next you will create a workspace folder called
c:\workspace which will be used to create the final executable jar file.
Create
a subfolder called work inside c:\workspace
Copy
the myapp subfolder from c:\source to
c:\workspace\work.
Since
you don’t want to package the java files in the final executable jar file, you
will go to C:\workspace\work\myapp\game and delete all the java files.
Important
point to remember: Preserve the package structure of the applet class file
while copying the class files to the c:\workspace\work folder. This roughly
translates to the following. If you have an applet inside a package called myapp.game, then copy the applet class file to
c:\workspace\work\myapp\game
Next copy all the required resource files into
c:\workspace\work folder assuming all the resources are accessed in the program
from the root folder. However, if you use a code snippet as follows to access
the image file icon.jpg,
getImage (getDocumentBase
(),"res/icon.jpg");
Then
copy icon.jpg
to c:\workspace\work\res.In our case, since we don’t use relative folder to
access resources, we will copy ball.gif to
C:\workspace\work folder.
Next run the Applet2App and enter the information as
follows.
Click on the File Chooser button next to Applet Class
label and select the bricks class stored in C:\workspace\work\myapp\game
directory.
Click on the Directory
Chooser button next to Applet Root Folder and select c:\workspace\work as the
root folder. You will see that the Applet class text field gets changed to myapp.game.bricks which is(and
should be) same as the Applet code tag as specified in bricks.html.
Click on the
Target Folder Directory Chooser button and select c:\work as the final
directory where the executable jar file will be created.
Next modify the
applet parameter table. Here you specify the applet parameters like width,
height etc. The default parameters are as follows.
appTitle: Title of the Application which will appear on the
title bar of the application
appIcon: Application icon, I copied a file called bricks.jpg in c:\workspace folder and specified bricks.jpg as the application icon.
appWidth: Width of the applet, same as the width parameter in
the applet tag of bricks.html.
appHeight: Height of the applet, same as the height parameter in
the applet tag of bricks.html.
appStatusbar: this is used to specify if the status bar will be viewable
or not. Default is “yes’.
Apart
from the above parameters, you can add your own applet specific parameters
(found as the PARAM=VALUE pairs in the APPLET Tag). Our application does not
use any extra parameters. So we didn’t define any other parameters.
Now
you should click on “Create Jar” button. Before clicking on the “Create Jar”
button, ensure that jar utility which comes as part of the JDK, is included in
PATH. A Jar file(bricks.jar)
and an INI (bricks.ini) should be generated in c:\workspace folder. Delete
c:\workspace\work folder. This is no longer required.
Now
you should be having only 3 files in c:\workspace folder. These are
bricks.jar: the executable Jar file
bricks.ini: the configuration file for bricks.jar
bricks.jpg: the application icon for bricks.jar
Click
on bricks.jar file and you should see a screen
similar to the following screen.
One
pleasant side effect is, you can use the same executable jar file as the applet
archive file in the original html page. In our case, we need to modify
bricks.html (modifications marked in RED) as
follows.
<HTML>
<BODY>
<CENTER>
<P>
<APPLET code="myapp.game.bricks"
archive=”bricks.jar” width=640 height=392></APPLET>
</P>
</CENTER>
</BODY>
</HTML>
Delete
all the class files and copy the bricks.jar file to
the same folder as bricks.html. Then click on bricks.html file. The applet
should start running.
You can not use Applet2App to create the
Windows executable. However, I have developed an application called JarWrapper
which can be used to wrap an executable jar file in a Windows exe file. You
might want to check the FAQ to find out whether
you need it. However, JarWrapper is not a Open Source Software. But it’s a freeware. You can
download it and use it to convert your executable jar files to windows
executables absolutely free of cost.
Once you have created the executable Jar
file from your applet class files, you can use JarWrapper
to wrap it in a Windows executable file.
Resource files can be divided into two
categories.
i.
Configurable
resources
ii.
Static resources
Configurable resources are resources
which end users can configure /customize as per their requirements. Typically
these are INI files/ properties files which can be modified by the end user.
Ideally these should be distributed separately along with the executable Jar
file in a deployable package to the end user.
Static resources are resources like
images/graphics, splash screens, data files etc. which should not be tampered
by the end user. These should be embedded in the final executable jar file. The
applet developer should write his code in such a way that even though the
static resource is embedded in the jar file, the application should be able to
find it out. One example could be as follows.
Let’s say, in the existing applet code,
you have the following piece of code which creates an ImageIcon
from a JPG file as follows.
ImageIcon anIcon = new ImageIcon ("res/icon.jpg");
If
the above code is present in you’re applet code, then you can not deploy icon.jpg file embedded inside your executable jar file.
This won’t work. But you can release it as a separate configurable resource as res/icon.jpg. However, if you do the following modification
and recompile your applet code, then you can embed icon.jpg
file in the res subfolder of executable jar file.
ImageIcon
anIcon = new ImageIcon(new URL(getDocumentBase (),
"res/icon.jpg"));
The thumb rule is: if you want to access
a static resource embedded inside the
executable jar file, then get the URL of the JAR file’s Root by calling getDocumentBase() or getCodeBase()
and then use the relative path of the resource inside the JAR file
to access it.