Tuesday, March 24, 2009

Creating installer for Java application using Visual Studio Setup project

In this article we will get familiar how to create custom installer for non .NET based application using Visual Studio Setup project technology.

It is known that Setup project in VS 2005/2008 gives way to easy creation of msi installer for your .NET application, automatically detect decencies, execute custom actions, etc. In this post I will show one unusual case where this technology can be used – creating installer for Java application. There are steps in this task:

  1. Ensure that correct version of Java is installed on the machine and install Java if needed
  2. Copy the application in the chosen folder and create shortcut to the desktop.

  1. Check the target machine for Java installed and install Java if needed – way 1

First we need to check if our machine has installed the correct version of JRE. In our case we will check if Java version 1.6 is installed on the machine. To do this we will check target machine registry records. First let’s see what .NET offers us. We can use the Section Launch conditions in our Setup project to check the registry:

Go to launch condition section, right click on the Search Target Machine and add Search registry node. Then set its properties to search the current Java version. The key we are looking for is HKEY_LOCAL_MACHINE\SOFTWARE\JAVASOFT\JAVA RUNTIME ENVIRONMENT /CURRENTVERSION.

Next we can add Launch condition. We add condition for java version based on the found registry entry. We are looking for Java 1.6 or higher. We specify the error message and the installation URL.

This is some way to deal with the situation but it does not actually installs JRE rather than preventing program install if the requirement are not satisfied. If you run this installer on machine without Java you will need to manually install the JRE after its installation is complete.

  1. Check the target machine for Java installed and install Java if needed – way 2

The second way is to use one of the benefits of the setup projects – creating custom action. It allows you to trigger event during installation. Here we can call the installer directly by selecting the path to the .exe. We can add condition to start this exe and again we will use search result from registry search “JAVAVERSION” from the previous example.

We can add the desired file in our project through the file system editor and then executed it as a custom action. Everything seems quite simple, but wait, there is a catch – JRE 1.6 is MSI installer. That means that you cannot run it while our MSI is running. As a result we can use this approach when we are running non-MSI installers. So what to do then?

  1. Check the target machine for Java installed and install Java if needed – way 3

What if we have MSI installer, containing software that is vital for our program? The decision is called prerequisites. As we know when we add .NET project output in Setup project it automatically detect its dependencies and add them as prerequisite. So, we will use this mechanism to add Java as prerequisite. We can view the current project prerequisites from Project -> Properties -> Prerequisites. Currently only .NET Framework and Windows installer will be selected. We will deselect them, since our project will not .NET. If we look at the list we won’t find JAVA as prerequisite (what a surprise :). But we can add it. All we need is to create package manifest. For this we will need Bootstrapper Manifest generator. The one for VS 2008 can be found here. So, after installation we open it and choose New -> Package Manifest.

Next we name the project.

After that we can add the file for Java installation from the top-left file – the installer icon. Next we choose the install file we need to add.

After that we need to add the system registry check. We choose System check tab and add new registry check. We will search for the same registry record as before.

Then we add display name of the install file is its settings. We name it Java. And the package is ready. There are also many other settings that can be set. Check whether folder named Java is located in C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bootstrapper\.

And now we go back to Prerequisites menu of our project we will se that our Java is in the list. Check it and select the second option from the radio button list. That way your these installations will be copied in the output directory.

Now we can add the Java application file in the application folder and finish installation as it is .NET based.

One more thing, creating the desktop shortcut mechanism is not obvious – creating new shortcut directly in the folder gives you only option to choose folder. To create shortcut to your file you need to right click on your file and choose create shortcut. Then you can move it to the desktop.

In this we saw several ways to extend the ability for creating flexible installer. This approach is not limited to Java or .NET applications but can be used on other technologies as well.

1 comment:

rafaƂ said...

Instruction from pint 3 don't work. I tried this in Visual Studio 2005.
In field Registry Value should be only the the register value (CurrentVersion). Here application only reads value from the register.
Verision of JRE should be check in section Install Condition for the added install file.
For example:
If we need version JRE 1.6 or higher, then in this section should be:
Type -> ByPasslf, Property ->CurrenVersion, Comparision >= and Value -> 1.6.
Good practis is set the Exit Codes (for the successful installation exit code = 0).

P.S. Sorry for my english.