How to select the JRE version used by the Java Launcher

ImportantThe following article uses options that are available starting with the Enterprise edition and project type.

Advanced Installer allows you to set the Java Runtime Environment versions which can be used by your Java application through the Settings tab of the Java Products page. However, you may want to allow your users to select the JRE version when the package runs.

Configure the installation package

Before implementing the feature which allows the user to select a JRE version, you need to configure your Advanced Installer project. For this you can follow these steps:

  1. create an Enterprise or Architect project
  2. go to the Files and Folders page and add your Java Application
  3. go to the "Java Products" page and create a Java product for your application
  4. go to the "Files and Folders" page and create a new folder under the folder which contains the Java Launcher and its INI
  5. in this sub-folder create a new INI file
  6. in the New INI File dialog create a new section and name it Java Runtime Environment
  7. under this section create a new entry
  8. set the Key field to JRE Path
  9. set the Value field to [JRE_COMBO]
  10. rename this custom INI to the name of the INI used by the Java Launcher
  11. double-click the folder which contains the custom INI and set the "Install folder content into the parent folder" option

The custom INI file will use the "JRE_COMBO" property to tell Java Launcher the path of the JRE it must use. The entries in this INI file will be added to the INI used by the Java Launcher because of the "Install folder content into the parent folder" option.

Create the custom actions

In order to get the JRE versions installed on the system and place them into a combo box, you can use two custom actions: a VBScript custom action which reads the JRE versions from the registry and the predefined PopulateComboBox custom action. The custom action which enumerates the registry keys under HKLM\SOFTWARE\JavaSoft\Java Runtime Environment adds the JRE versions and their paths to the AI_COMBOBOX_DATA property. This property is used by the "PopulateComboBox" custom action to populate the JRE_COMBO combo box.

Here is a sample VBScript custom action which reads the registry and sets the "AI_COMBOBOX_DATA" property (I called it "getJreVersions.vbs"):

'declare and initialize the variables
Dim ComboString
ComboString = "JRE_COMBO" & "|"

Dim text(10)
Dim value(10)

const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."

'set the path of the registry entry which contains the JRE versions
Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\"&_
    strComputer & "\root\default:StdRegProv")
strKeyPath = "SOFTWARE\JavaSoft\Java Runtime Environment"
strValue = "JavaHome"

'get the JRE versions
objReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubKeys

'place the JRE versions in two containers
indexReg = 1
For Each strKey In arrSubKeys
	strJavaHome = strKeyPath & "\" & strKey
	objReg.GetStringValue HKEY_LOCAL_MACHINE, strJavaHome, strValue, javaPath
	text(indexReg) = strKey
	value(indexReg) = JavaPath
	indexReg = indexReg + 1
Next

indexReg = indexReg - 1
indexCombo = indexReg

'create the string for AI_COMBOBOX_DATA
For indexCombo = indexReg to 1 Step -1
	ok = "not present"
	j = indexCombo + 1
	'make sure that the values are unique
	For i = indexReg to j Step -1
		if value(i) = value(indexCombo) then
			ok = "present"
		End if
	Next
	if ok="not present" then
		ComboString = ComboString & value(indexCombo)& "#" & text(indexCombo) & "|"
	End if
Next

'set the AI_COMBOBOX_DATA property
Session.Property("AI_COMBOBOX_DATA") = ComboString
'set the default value of the combo to the latest JRE version
Session.Property("JRE_COMBO") = JavaPath

Basically, here is what happens when this custom action is executed

  1. it enumerates the registry keys under the HKLM\Software\JavaSoft\Java Runtime Environment registry key
  2. these registry keys are created for each JRE version installed on the system and their name contains the actual JRE version
  3. each registry key contains a value named JavaHome which contains the path of the JRE version
  4. the first step is to place each JRE version and its path in two string arrays (text for the version and value for the path)
  5. after this, the custom action goes through these arrays from the end towards the beginning
  6. this is done because the JRE versions are ordered from the lowest to the highest (we need them ordered from the highest to the lowest)
  7. each value pair from these arrays is added to a string variable called ComboString
  8. when adding the values, the custom action verifies for duplicate entries (for example, the path of the 1.6 version is the same as the path of the newest JRE 1.6 update)
  9. if a duplicate entry is found, it is not added to "ComboString"
  10. after "ComboString" has been built, we set it as the value of the AI_COMBOBOX_DATA property
  11. the final step is to set the "JRE_COMBO" property to the last path that was read from the registry (this will set the default value of the combo box to the newest JRE version installed on the target machine)

Configure the User Interface

After configuring the Java product and creating the custom action, you need to configure the UI of your installation package. Here are the steps you must take:

  1. go to the Custom Actions page
  2. add the custom action you created as a "custom action without sequence"
  3. make sure this custom action has the "Function Name" field empty
  4. under this custom action, add the Populate combo box predefined custom action
  5. go to the Dialogs page and create a custom dialog (or you can modify an existing one)
  6. add a combo box control
  7. select the combo box and set the Property Name field in the Properties pane to JRE_COMBO
  8. under the Init Events tab of the "Control" section add these control events:
Execute custom action         getJreVersions.vbs           1
Execute custom action         PopulateComboBox             1

Sample project

You can download a sample project from here.