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.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...