April 24, 2009
Recently I had to help some remote users setting up one of those Java applications that requires a JAVA_HOME environment variable pointing to the base directory of the Java SDK.
The manual process is not that complex, but apparently it can be very mind-boggling to a less versed computer user. It seems that the biggest problem is to find the correct value for the variable.
To overcome the problem I created an AutoIt script that reads the Java Home path from the registry, presents a confirmation dialog to the user and then sets the environment variable by creating or changing some registry keys.
AutoIt scripts can be compiled into a standalone executable so the user's task becomes as easy as double clicking in a file.
Here's the confirmation dialog shown by the script:
The entire AutoIt script code:
#include <WindowsConstants.au3> #include <GUIConstantsEx.au3> ; Obtain the JDK current version from HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Development Kit\CurrentVersion $currentversion = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Development Kit", "CurrentVersion") If @error <> 0 Then MsgBox(0, "Error", "The Java Development Kit installation path could not be determined") Exit EndIf ; Obtain the JavaHome path from HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Development Kit\<current version>\JavaHome $javahome = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Development Kit\" & $currentversion, "JavaHome") If @error <> 0 Then MsgBox(0, "Error", "The Java Development Kit installation path could not be determined") Exit EndIf ; ; Create the GUI ; ; Enable event notifications: Opt("GUIOnEventMode", 1) ; Create main window GUICreate("Set Environment Variable", 400, 165, -1, -1, $WS_DLGFRAME) GUICtrlCreateLabel("Name:", 10, 10) GUICtrlCreateLabel("JAVA_HOME", 60, 10) GUICtrlCreateLabel("Value:", 10, 30) GUICtrlCreateLabel($javahome, 60, 30) GUICtrlCreateLabel("Scope:", 10, 52) $radiouser = GUICtrlCreateRadio("USER", 60, 50, 120, 20) $radiosystem = GUICtrlCreateRadio("SYSTEM", 60, 70, 120, 20) $okbutton = GUICtrlCreateButton("OK", 90, 100, 100) $cancelbutton = GUICtrlCreateButton("Cancel", 210, 100, 100) ; Set event handlers GUICtrlSetOnEvent($okbutton, "OnOk") GUICtrlSetOnEvent($cancelbutton, "OnCancel") ; Check SYSTEM GUICtrlSetState($radiosystem, $GUI_CHECKED) ; Set focus on the OK button GUICtrlSetState($okbutton, $GUI_FOCUS) ; Show main window GUISetState(@SW_SHOW) ; Prevent the application from closing (button event handlers will terminate the app) While True Sleep(1000) WEnd ; ; Functions ; ; OK button event handler Func OnOk() ; Disable buttons GUICtrlSetState($okbutton, $GUI_DISABLE) GUICtrlSetState($cancelbutton, $GUI_DISABLE) ; Set the environment variable If BitAND(GUICtrlRead($radiouser), $GUI_CHECKED) = $GUI_CHECKED Then ; USER scope $result = RegWrite("HKEY_CURRENT_USER\Environment", "JAVA_HOME", "REG_SZ", $javahome) Else ; SYSTEM scope $result = RegWrite("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment", "JAVA_HOME", "REG_SZ", $javahome) EndIf If $result = 1 Then ; Refreshes the OS environment EnvUpdate() ; Notifies user of success MsgBox(0, "Information", "JAVA_HOME was set to '" & $javahome & "'") Else MsgBox(0, "Error", "Error setting the environment variable") EndIf Exit EndFunc ; Cancel button event handler Func OnCancel() Exit EndFunc
You can download the script here.
- KB104011 - Explains how to set environment variables using the registry and how to propagate changes to the system
- KB821761 - Explain why changes made to environment variables do not affect services that run under the Local System account until Windows is restarted
Deploying the JRE on Windows - Explains the registry keys created by the JRE installer (the JDK registry keys are similar but instead of residing inside
HKEY_LOCAL_MACHINE\Software\JavaSoft\Java Runtime Environmentthey are inside
HKEY_LOCAL_MACHINE\Software\JavaSoft\Java Development Kit)