As a developer one likes to have applications that can be distributed in
easy, installable setups. Since the installation setup is the first
interaction of the user with your application, you would like to have all
installation tasks be done like a breeze. Nullsoft has come out with a
script-based
program, NSIS, that helps you create such an installer (i.e. setup.exe).
Nullsoft Scriptable Install System (NSIS) is an Open Source package that
allows programmers to make Windows-based installers for their applications. NSIS
has two components-script and a compiler. In order to create an installer for
your software, you need to write a script which will decide as to how the
installer will behave. For example, the path for installing the software and
what all registry changes it needs to have.
|
Plus, the shortcuts it will create on the desktop.
Once you have written the installer script, you need to compile it in the
given compiler, which will give you the final distribution as a 'setup.exe'
file. Installer created with NSIS is compatible with all versions of Windows (be
it 95 or Vista).
Getting started
We have given the NSIS package installer on this month's PCQ Professional CD.
Copy the NSIS package installer on your Windows machine first. After installing
the NSIS package, you need to create a script. For this open a Note pad and
start writing the installation script for your software. Let's see how a script
to build an installer for a JAVA program works.
It makes an installer setup for a simple Java program that does the following
tasks:
- Copies all class files into the installer package.
- Creates Start Menu icons for running the demo application.
- Creates a Desktop shortcut for running the demo application.
- Checks whether JRE is present or not.
- Creates registry entries for uninstallability of application through
Add/Remove programs. - Makes the application uninstallable.
The compilation is easy. You just have to drag and drop your script file onto the compiler window |
The first few instructions of the script define the installer attributes. The
attributes that are set here are the window names and the text to be displayed
on them. Let's name our application as 'PCQ Application' and its installer as 'PCQAppInstaller.exe.'
The following code snippet shows the same:
Name "PCQ Application"
Caption "PCQ Demo Application"
OutFile "PCQAppInstaller.exe"
DirText "Please choose a directory to which you'd like to install PCQ
Application."
InstallDir "$PROGRAMFILES\PCQ Application"
The 'OutFile' keyword gives the name to the setup program that will be
created after compilation and 'InstallDir' creates default location for the
installation process. NSIS has some constants like $PROGRAMFILES, $DESKTOP and $SMPROGRAMS
which point to the current user's Program Files folder, desktop and Start Menu
respectively.
Each script has at least one section. A section carries the instructions for
different segments of installation process. Our script has two sections, namely
Install and Uninstall. The Install section will have instructions to copy files
into installer package, make their shortcuts and registry entries. The code for
this is as follows.
Section "install"
SetOutPath $INSTDIR\
File E:\nsis_scripts\java_app\PCQ_Test.class
...
WriteUninstaller $INSTDIR\Uninstall.exe
CreateDirectory "$SMPROGRAMS\PCQ Application"
CreateShortCut "$DESKTOP\PCQ Application.lnk" "$SYSDIR\javaw.exe" "PCQ_Test"
...
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\PCQ
Application" "DisplayName"\ "PCQ Application (remove only)"
...
SectionEnd
PCQAppInstaller when installed on a computer will create Desktop and Start Menu shortcuts and the program PCQ_Test could be executed from these shortcuts |
'SetOutPath' sets the location where the files will be copied upon
installation and 'File' keyword packs the files into the installer. The Start
Menu program setup for the PCQ Application and the registry entries for the same
will be done through 'CreateDirectory' and 'WriteRegStr' respectively. Now if we
look at the 'CreateShortCut' instruction, we have three parameters. The first
will create the desktop shortcut link for the PCQ Application, second will
invoke JRE's javaw.exe to execute the class name passed as third parameter i.e.
PCQ_Test. Also, we have to check whether JRE is present on users' machine or not
as it is needed to execute our Java program. So, we can add the following
snippet to 'install' section to do this check.
readRegStr $0 HKLM "SOFTWARE\JavaSoft\Java
Runtime Environment" CurrentVersion
StrCmp $0 "" NoFound
...
NoFound:
MessageBox MB_OK "JRE not found. Installation will quit now...
The $0 variable, if blank, shows no JRE is installed otherwise it will store
the JRE version. Like other programming languages, conditional checking can be
done. 'StrCmp' lets us check for value in $0 variable. If blank, it jumps to
NoFound case else it continues with installation. This way the installation
setup will be prepared.
Finally to make the application uninstallable, we have to create a section
'uninstall.' In this section we give instructions to delete all shortcuts, files
and their directories, and also to remove the registry entries. We put the
instruction WriteUninstaller $INSTDIR\Uninstall.exe in the install section. Now
NSIS will look for an uninstall section in the script while compiling. The
snippet for uninstall script is as follows.
Section "Uninstall"
...
Delete $INSTDIR\PCQ_Test.class ;//delete files
Delete "$DESKTOP\PCQ Application.lnk"
...
SectionEnd
If JRE is not present the error prompt will be displayed and installation will quit for the PCQ Demo Application |
Once you have finished writing the script, save the file with .nsi extension.
Now lets also see what's in PCQ_Test. java program. It takes a date instance
from the system time and displays the current date and time to the user after
formatting it and converting that to a string. The Ccode snippet is as follows:
public PCQ_Test() {
Date today;
DateFormat dateFormatter;
dateFormatter = DateFormat.getDateInstance(DateFormat.DEFAULT);
DateFormat timeFormatter =
DateFormat.getTimeInstance(DateFormat.DEFAULT);
today = new Date();
String showDate = dateFormatter.format(today);
String showTime = timeFormatter.format(today);
String message = "Hello!! Today's Date is "+showDate+" and Time is "+showTime;
...
}
Compilation
Now to open NSIS compiler, launch 'MakeNSISW' from Start>Programs> NSIS. This
will open a complier window. To compile the script that you have created above,
load the script by clicking File>Load Script option. Alternatively, the script
can also be dragged onto the compiler window or by right-clicking the .nsi file
and choosing 'Compile NSIS Script.' The compiler compiles the script and if it
does not find any errors with the instructions the 'Test Installer' button gets
enabled. If script has errors they are shown on the window and the build process
is stopped. With the 'Test Installer' button we can test the installer setup
that the script has created.
In conclusion
NSIS not only makes installers for Java developers but also for any other
application development environment, be it Macromedia Flash or .NET. Thus it's a
handy tool for developers to make their applications easy for distribution.