Answer

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:

  • create an Enterprise or Architect project
  • go to the Files and Folders page and add your Java Application
  • go to the "Java Products" page and create a Java product for your application
  • go to the "Files and Folders" page and create a new folder under the folder which contains the Java Launcher and its INI
  • in this sub-folder create a new INI file
  • in the New INI File dialog create a new section and name it Java Runtime Environment
  • under this section create a new entry
  • set the Key field to JRE Path
  • set the Value field to [JRE_COMBO]
  • rename this custom INI to the name of the INI used by the Java Launcher
  • 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

  • it enumerates the registry keys under the HKLM\Software\JavaSoft\Java Runtime Environment registry key
  • these registry keys are created for each JRE version installed on the system and their name contains the actual JRE version
  • each registry key contains a value named JavaHome which contains the path of the JRE version
  • 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)
  • after this, the custom action goes through these arrays from the end towards the beginning
  • 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)
  • each value pair from these arrays is added to a string variable called ComboString
  • 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)
  • if a duplicate entry is found, it is not added to "ComboString"
  • after "ComboString" has been built, we set it as the value of the AI_COMBOBOX_DATA property
  • 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:

  • go to the Custom Actions page
  • add the custom action you created as a "custom action without sequence"
  • make sure this custom action has the "Function Name" field empty
  • under this custom action, add the Populate combo box predefined custom action
  • go to the Dialogs page and create a custom dialog (or you can modify an existing one)
  • add a combo box control
  • select the combo box and set the Property Name field in the Properties pane to JRE_COMBO
  • 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.