Tuesday, January 24, 2012

Making an Installer EXE from Java Application

In any software installation processes, firstly it involves bundling the necessary  files into one file may be jar file or an EXE (I'll show you how we can convert executable Jar file a windows executable file later in this article) and secondly it involves copy the bundled files into required location in your computer one by one. I will explain how we can make such EXE containing bundled jar files, resource files etc which is based on Java application.

The main part to achieve this is that we need to write a logic that reads the executable Jar file and copies the files into required location and these java class files should also be packed into one jar file where all other installation files will be present. (I'll explain the logic to do this after explaining packaging files)

First thing involves bundling the necessary files into one single jar file. Place all the files required to be installed into one location for example: D:\Packaging.
Place create a menifest file with the entry Main-Class: Installer and place this under packaging folder.

Issue following command to pack all the files into one Jar file.

D:\Packaging\jar cvfm installer.jar *.menifest *.class *.jar *.txt  (Make sure you include all file types you wanted to pack)

Creating Installer Java class with installation logic.

Create a simple java project using any IDE and create a class called as Installer in the default package (Only default package worked for me, you can try out differently)
Installer class must have main method where the execution of logic starts. You can have UI code that displays UI dialogs when the executable jar is double clicked. For example a UI dialog to show End User License Agreement etc and progress dialog as shown below.



Inside the Installer class, i'll create an inner class where the logic for reading the file and copying to required location is be given. this is going to happen in a swing worker thread

class InstallTask extends SwingWorker {
    public Void doInBackground() {         
        int progress = 0;         
        setProgress(0);
        while (progress < 100) {              
            try{                  
                URL url = this.getClass().getResource("<one-bundled-file-name-here>");
                String file = url.getFile();
                file = file.substring(6, file.indexOf("!"));
                String strJustFileName = file.substring(file.lastIndexOf("/") + 1);
                JarFile jarFile = new JarFile(strJustFileName);                                       
                Enumeration entriesEnum = jarFile.entries();                   
                while (entriesEnum.hasMoreElements()) {
                    JarEntry entry = (JarEntry)entriesEnum.nextElement();                       
                    if (entry.isDirectory()) {
                        continue;
                    }                             
                    String fileName = entry.getName();
                    InputStream inputStream = null;

                    if(fileName.contains(".jar")){ //Its a jar file
                        inputStream = jarFile.getInputStream(entry);
                        //Have the logic in this method to copy the file
                        //into required target-path
                        installFile(inputStream, "TARGET-PATH", fileName);
                    }
                }
            }catch(Exception e){
            }
        }
    }
}

Do a clean build of this project and bundle the class files created while packaging.
Now, when you double click on this jar file, your UI should come up (i've not shown code for creating this UI, write logic to create the UI you wanted and called InstallTask class may be when user clicks on 'Install' button)

Now, double click on the output jar file, your UI should open up guiding further installation.

Coverting executable java jar file into Windows native Executable file (with .EXE extension)

For this we need a third party wrappers for java application, the one i used is Launch4j (http://launch4j.sourceforge.net/).
Launch4j is open source software, download and install it in your system. When you launch this software, the UI will guide you to make EXE out of java jar file.

Lanuch4j provides an ant target which can be used for automating the process of wrapping Java Applications into executable exe files.

Understanding GWT

What is GWT?

GWT is an open source Java development framework for developing Ajax based internet applications
Write, run, test, and debug everything in Java!
GWT Compiler translates Java code into JavaScript that is compatible with any browsers
An Open Source licensed under Apache 2.0 license

GWT Features

Dynamic & Reusable components
Simple RPC
Real Debugging
Browser Compatible
JUnit integration
Internationalization
JSNI – Java Script Native Interface 

Inside GWT

GWT comprises of mainly two components one is Class Libraries and another Development Tools.
The following picture gives more idea.



GWT User Interface

GWT UI comprises of Widgets, Layouts based on Panels (vertical, horizontal, scroll, tab etc.) and
Event Handlers/Listeners (Change, Click, Focus, Keyboard, Mouse, Scroll, Tab) 

How does GWT work?

GWT makes the web pages asynchronous unlike traditional html based web sites. Have a look at this picture.




GWT RPC



Running GWT Applications

GWT applications can be run in two modes
    1. Hosted mode
        -Runs as java byte code within the  JVM
        -Use this mode for debugging
    2. Web Mode
        -Runs as pure JavaScript

What does GWT supply?

GWT ships with following utilities
-Command-line utilities:
-projectCreator – make a project
-applicationCreator – generates starter application
-i18nCreator – generates internationalization scripts
-junitCreator – generates JUnit test & scripts for testing
- Platform Development Jar:  gwt-dev-xxx.jar ( where xxx is win32, linux, mac )
- Deployment Jar: gwt-user.jar
-  Sample Applications
- API Documentation 

GWT project structure
 com/example/app             -  Root project package. This package contains all the module files.
com/example/app/client   - Sub-package containing all the client-side source code.
com/example/app/server - Sub-package containing all the server-side source code
com/example/app/public  - Static resources



What is GWT Module?

A module bundles together all the configuration needed to use your code in a GWT project.
XML descriptor file ending with the .gwt.xml extension The contents include:
Inherited modules
An entry point application class name
Source Path entries
Public Path entries  

Working of GWT Application


 
Page loading in hosted mode

1. The Shell program opens a hosted browser window, which loads MyApp.html.
2. MyApp.html loads gwt.js with a <script> tag.
3. gwt.js scans MyApp.html and parses out the <meta name=’gwt:module’> to get the module name.
4. GWT reads the module file (MyApp.gwt.xml) to find the name of the EntryPoint class (MyApp).
5. The MyApp class is instantiated and its onModuleLoad module is called. Your application begins.
6. Your application code makes calls into the GWT run-time library (gwt-user.jar), which is also Java code.
7. Code in gwt-user.jar manipulates the hosted browser’s DOM to add UI components to the web page, and redirects all browser events back to the Java application code. 

JSNI
Writing Native Java Script Methods
public static native void alert(String msg)
     /*-{$wnd.alert(msg); }-*/;
Syntax:
    [instance-expr.]@class-name::field-name  

Server communication
Server communication happens through RPC, HTTP Requests and Gdata feeds (In JSON format)



What is GData?
A new standard Google protocol for reading and writing data on the web
GData is a new protocol based on Atom 1.0 and RSS 2.0.
To send a query to a server and receive a response containing a list of matching results
Blogger, Calendar, Spreadsheets, and Google Base can provide feed data in JSON format 

Advantages

No JavaScript syntax errors - No JavaScript programming at all!
Any java development tools can be used for creating GWT applications, for example Eclipse etc
Can use complex Java on the client
Reduced development time
The JavaScript generated out of GWT applications is cross browser compliant
Set of  reusable widgets
Less load on the server since all the UI generation happens at the client side
Better performance (Less network load)
GWT applications are comparatively  fast
Better end user experience with GWT applications
Ability to interface hand written JavaScript into java code (JSNI)
Excellent browser-to-server RPC 

Limitations

Big learning curve
Poorly documented process to deploy on a regular Java-based Web servers.
You never put direct JavaScript in your HTML. Instead, you use JSNI to wrap JavaScript in Java.
Most Ajax environments use JavaScript on the client and have a choice for the server. GWT is based entirely around Java.
Not all features of Java are supported in GWT. Example: multithreading
Client side code should be Java 1.4 or older compatible
Licensing terms & conditions
It is still evolving technology, advanced features like zooming, animations are not present currently.
Security issue
Related Posts Plugin for WordPress, Blogger...