Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Create a Native Installer for your NetBeans Platform App

DZone's Guide to

Create a Native Installer for your NetBeans Platform App

Have a NetBeans app but aren't sure if a client machine has JRE on it? Package it with your app using one of the two methods described here.

· Java Zone ·
Free Resource

Java-based (JDBC) data connectivity to SaaS, NoSQL, and Big Data. Download Now.

The ubiquity of Java makes it a platform of choice for building applications that can perform reliably under Big Data, mobile, and IoT environments. The Internet is filled with blogs, tutorial, and manuals that indicate the popularity of Java for developing modern applications. On the other hand, there is limited information available in publications that talk about applications of Java in the manufacturing Industries. In a typical manufacturing industry scenario where control rooms are equipped with the latest state-of-the-art facility to operate a plant, it may come as a surprise to you that there is hardly any computer that has Java installed on it! 

NetBeans IDE provides an easy way to create an installer for your NetBeans Platform projects. The built-in installer, however, is Java-based and requires a certain version of Java pre-installed on client computers. This forces you to follow a two-step approach, wherein before, proceeding for application installation, one has to first install a version of JRE that is capable of launching the installer. For example, if your application is built on top of NetBeans 8, then the client machine requires Java 7 preinstalled on it.

Fortunately, NetBeans allows you to extend the build process to suit your requirements. In the following paragraphs, I will show you how to package your application using the Inno Setup native installer for Windows. I will also provide scripts that you can modify and use to automatically generate the native installer in your NetBeans project dist folder. 

I figured out two one-step approaches that allow applications to run on client machines and where no pre-installed Java is required. 

  1. Use a native installer to bundle the JRE files along with the application files.

  2. Use a native installer to bundle only the application files and download and copy the JRE files from a web server during the course of the installation.

I will be demonstrating both methods below. I used Inno Setup installer for Windows to create my native installer.

Prerequisites

  1. Netbeans 8.1

  2. Java Development Kit 8

  3. Your NetBeans Platform project

  4. Inno Setup Compiler version 5.5.9 (a)

  5. GNU Wget 1.19.1 built on mingw32

  6. UnZip for Windows

Approach 1

Step 1: Modify your NetBeans platform project.properties File

At end of the file, add the line:

jdkhome="jre"


Step 2: Make Changes in Your Project build.xml File

Copy and paste the below text somewhere within the <project> open and close tags.

  <target name="build-zip" depends="suite.build-zip">

        <property name="release.dir" value="${dist.dir}/${app.name}"/>

        <unzip src="${dist.dir}/${app.name}.zip" dest="${dist.dir}"/>      


        <delete file="${dist.dir}/${app.name}.zip"/>

        <echo message=" "/>
        <echo message="cleaning and finalizing release." />

        <echo message="Copying utility tools to application root in dist" />

        <copy todir="${dist.dir}/${app.name}">
            <fileset dir="../<handytools>">               
            </fileset>  
        </copy>       

        <echo message="Copying JRE folders" />
        <mkdir dir="${dist.dir}/${app.name}/jre"/>

        <copy todir="${dist.dir}/${app.name}/jre">
            <fileset dir="../<jre1.8.0_112-x86>">
                <include name="**/bin/**"/>                
                <include name="**/lib/**"/>                                               
            </fileset>  
        </copy>  
     <exec executable="cmd" dir=".">
            <arg value="/C"/>
            <arg value="C:/Program Files (x86)/Inno Setup 5/iscc"/>
            <arg value="<myapp>-windows-x86.iss"/>
        </exec>  
    </target>


In the file above, the folder <handytools> contains the tools wget.exe, unzip.exe, and updatejre.bat, which I will be using in Approach 2. The folder named <jre1.8.0-112-x86> contains the 32-bit JRE lib and bin folders, which have to be packaged with your native installer. Make the necessary changes where value strings contain <angle> brackets. The above Ant target extends command "package as zip distribution" to automate following tasks:

  1. Unzip the application's ZIP distribution created in the dist folder.

  2. Delete the ZIP file and copy the contents of folder <handytools> into the unzipped folder created above. The unzipped folder is your application root folder <myapp>.

  3. Create folder jre in the application root and copy the lib and bin folders from folder <jre1.8.0_112-x86> into it. 

  4. Invoke command line Inno Setup Compiler to create the installer <myapp>-windows-x86.exe.

Step 3: Install Inno Setup for Windows

Download Inno Setup from here. Start it and create a new empty script file. Paste the content from the box below into the editor and save it as <myapp>-windows-x86.iss in your NetBeans projects folder. Make the necessary changes at the places indicated in the script within <angle> brackets. This script will create a desktop shortcut on the client machine and also launch it after installation completes.

; -- <myapp>-windows-x86.iss  --
; Demonstrates Installing NetBeans Platform Application files and creating a desktop icon.

; SEE THE DOCUMENTATION FOR DETAILS ON CREATING .ISS SCRIPT FILES!

[Setup]
AppName=<myapp name>
AppVersion=1.0

DefaultDirName={%USERPROFILE}\Applications\<mycompany>\
DefaultGroupName=<mycompany>
UninstallDisplayIcon={%USERPROFILE}\Applications\<mycompany>\<myapp>\<myapp>-unins.exe
Compression=lzma2
SolidCompression=yes
;create installer in dist directory of your Netbeans Platform Project
OutputDir=E:\asutosh\<myproject>\dist\
OutputBaseFilename=<myapp>-windows-x86

[Files]
;unzip the zipped distribution in dist directory and include all 
;files in root directory of the application.

Source: "E:\asutosh\<myproject>\dist\<myapp>\*"; DestDir: {app}; Flags: ignoreversion recursesubdirs

[Icons]
Name: "{commondesktop}\<myapp name>"; Filename: {%USERPROFILE}\Applications\<mycompany>\<myapp>\bin\<myapp>.exe

;launch application after install on client machine
[Tasks]
Name: StartAfterInstall; Description: Run application after install

;look for exe in this path on client machine
[Run]
Filename: {%USERPROFILE}\Applications\<mycompany>\<myapp>\bin\<myapp>.exe; Flags: shellexec skipifsilent nowait; Tasks: StartAfterInstall



If you wish, you can follow similar steps to build another installer that bundles the 64-bit version of JRE with your application.

Approach 2

Step 1: Create Zipped Bundles of JRE for Downloading From a Web Server

Go to your 32-bit JDK home installed on your computer; open the JRE folder and create a ZIP file containing the lib and bin folders. If you have a 64-bit version of JDK with you, then create another bundle and name it appropriately. Copy the two zipped files and host them on a web server.

For example, I created jre-x86.zip for Windows 32-bit and jre-x64.zip for Windows 64-bit, then hosted them on a web server. The zipped files will be downloaded by the installer using the paths http://<serverIP:port>/jres/jre-x86.zip and http://<serverIP:port>/jres/jre-x64.zip, respectively.

Step 2: Modify Your NetBeans Platform project.properties File

At end of the file, add the line:

jdkhome="jre"


Step 3: Make Changes in Your Pproject build.xml File

Copy and paste the below text somewhere within the <project> open and close tags.

  <target name="build-zip" depends="suite.build-zip">

        <property name="release.dir" value="${dist.dir}/${app.name}"/>

        <unzip src="${dist.dir}/${app.name}.zip" dest="${dist.dir}"/>      


        <delete file="${dist.dir}/${app.name}.zip"/>

        <echo message=" "/>
        <echo message="cleaning and finalizing release." />

<mkdir dir="${dist.dir}/${app.name}/jre"/>

        <echo message="Copying utility tools to application root in dist" />

        <copy todir="${dist.dir}/${app.name}">
            <fileset dir="../<handytools>">               
            </fileset>  
        </copy>      
 <exec executable="cmd" dir=".">
            <arg value="/C"/>
            <arg value="C:/Program Files (x86)/Inno Setup 5/iscc"/>
            <arg value="<myapp>-windows-x86.iss"/>
        </exec>  
    </target>


In the file above, the folder <handytools> contains the tools wget.exe, unzip.exe, and updatejre.bat, which I will be using in the step below.  Make the necessary changes where your parameter value strings contain <angle> brackets. The above Ant target extends the command "package as ZIP distribution" to automate the following tasks:

  1. Unzip the application ZIP distribution created in the dist folder.

  2. Delete the ZIP file and copy the contents of the folder <handytools> into the unzipped folder created above. The unzipped folder is your application root folder <myapp>.

  3.  Create an empty jre folder in the application root.

  4. Invoke command line Inno Setup Compiler to create the installer <myapp>-windows-x86.exe.

Step 4: Download and Create

Download wget.exe from https://eternallybored.org/misc/wget/ and unzip.exe from http://gnuwin32.sourceforge.net/packages/unzip.htm. Copy the files in the <handytools> folder on your computer.

Create a batch file, updatejre.bat, with content as shown below. Make the necesssary changes at the places indicated in the script within <angle> brackets. The script performs the following tasks sequentially during installation on a client computer:

  1. Checks for bitness of Windows OS and runs wget.exe to download the appropriate JRE in a zipped file from a web server into the installed application's home jre folder.

  2. Unzips the file downloaded above and deletes the ZIP file

  3. Launches NetBeans Platform application from the given path.

# check for bitness of Windows
if defined ProgramFiles(x86) (
SET jrezip="jre-x64.zip"
) else (
   SET jrezip="jre-x86.zip"
)
# start wget to download the jre in application root/jre folder
wget.exe --directory-prefix=jre http://<serverIP:port>/jres/"%jrezip%"

unzip.exe -d jre "jre/%jrezip%"

cd jre
del "%jrezip%" > null
del null
#launch Netbeans Platform App
start %USERPROFILE%\Applications\<mycompany>\<myapp>\bin\<myapp>.exe
exit 0


Step 5: Launch Inno Setup for Windows 

Start the Inno Setup software and create a new empty script file. Paste the content from below into the editor and save it as <myapp>-windows-x86.iss in your NetBeans projects folder. Make the necessary changes at the places indicated in the script within <angle> brackets.  This script will create a desktop shortcut on the client machine and also launch updatejre.bat file created above, after installation completes.

; -- <myapp>-windows-x86.iss  --
; Demonstrates Installing NetBeans Platform Application files and creating a desktop icon.
;Launches updatejre.bat 
; SEE THE DOCUMENTATION FOR DETAILS ON CREATING .ISS SCRIPT FILES!

[Setup]
AppName=<myapp name>
AppVersion=1.0

DefaultDirName={%USERPROFILE}\Applications\<mycompany>\
DefaultGroupName=<mycompany>
UninstallDisplayIcon={%USERPROFILE}\Applications\<mycompany>\<myapp>\<myapp>-unins.exe
Compression=lzma2
SolidCompression=yes
;create installer in dist directory of your Netbeans Platform Project
OutputDir=E:\asutosh\<myproject>\dist\
OutputBaseFilename=<myapp>-windows-x86

[Files]
;unzip the zipped distribution in dist directory and include all 
;files in root directory of the application.

Source: "E:\asutosh\<myproject>\dist\<myapp>\*"; DestDir: {app}; Flags: ignoreversion recursesubdirs

[Icons]
Name: "{commondesktop}\<myapp name>"; Filename: {%USERPROFILE}\Applications\<mycompany>\<myapp>\bin\<myapp>.exe

; launch updatejre.bat batch after install on client machine
[Tasks]
Name: StartAfterInstall; Description: Run application after install

;look for exe in this path on client machine
[Run]
Filename: {%USERPROFILE}\Applications\<mycompany>\<myapp>\updatejre.bat; Flags: shellexec skipifsilent nowait; Tasks: StartAfterInstall


Step 6: Run It

Right-click your NetBeans Platform project and run "clean and build", then run "package as ZIP distribution".

At the end of the process, you will find one application root folder <myapp> in the dist folder.The root folder contains, among other things, the empty jre folder and utility files wget.exe, unzip.exe, and updatejre.bat copied from the <handytools> folder. In the dist folder, in addition to your <myapp> folder,  you will see a <myapp>-windows-x86.exe created by the Inno Setup compiler. This file is the native installer, which does not require any JRE to be present on the client computer for installation and launching of your NetBeans Platform application!

In contrast to Approach 1, where a private JRE is bundled with the installer, in Approach 2, toward the end of the installation on the client machine, you will see the updatejre.bat script running in a console window, downloading and copying your private JRE from the web server!

Thanks for taking the time to read my article. Hope it was helpful!

Connect any Java based application to your SaaS data.  Over 100+ Java-based data source connectors.

Topics:
java ,netbeans ,jdk ,installer ,tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}