Inno Setup - setting a Java environment variable

I use Inno Setup to make an installer for the project I'm working on, and I need it to set a java environment variable so that when they run cmd they don't get a java error that is not found or something like this .

I found several other posts related to Inno Setup and installation environment variables, but does anyone know something specific for this instance?

+5
environment-variables inno-setup
May 21 '12 at 2:28 pm
source share
2 answers

Assuming Java is installed in the default program / Java files, something like this should work in your case:

[Registry] ; set PATH Root: HKCU; Subkey: "Environment"; ValueType:string; ValueName:"PATH"; ValueData:"{olddata};{pf}\Java\bin"; Flags: preservestringtype ; set JAVA_HOME Root: HKCU; Subkey: "Environment"; ValueType:string; ValueName:"JAVA_HOME"; ValueData:"{pf}\Java"; Flags: preservestringtype [Setup] ; Tell Windows Explorer to reload the environment ChangesEnvironment=yes 

I'm not sure which environment variable you want to set - PATH or JAVA_HOME - so I added both of them.

Changing PATH should not be necessary as the Java installer seeks to add itself to the path; IIRC copies java.exe and javaw.exe to one of the system directories.

+10
May 21 '12 at 14:46
source share

By adding an answer to @Joni, you can get the Java installation directory from the registry and use script constants to set your environment variable:

(EDIT: thanks @TLama for fixing the code!)

 [Registry] Root: HKCU; Subkey: "Environment"; ValueType:string; ValueName:"JAVA_HOME"; ValueData:"{code:GetJava32Path|6}"; Flags: preservestringtype [Code] const RegKeyJRE = 'SOFTWARE\JavaSoft\Java Runtime Environment\'; function GetJava32Path(MinVersion: string): string; var I: Integer; Path: string; Versions: TArrayOfString; begin Result := ''; if RegGetSubkeyNames(HKLM, RegKeyJRE, Versions) then for I := 0 to GetArrayLength(Versions)-1 do if (Versions[I][2] = '.') and (Versions[I][3] >= MinVersion) and RegQueryStringValue(HKLM32, RegKeyJRE + Versions[I], 'JavaHome', Path) then begin Result := Path; Exit; end; end; 

(NB I am far from being an expert in Pascal-script, the code can be much better, although now it was fixed by @TLama :))

+2
Apr 09 '15 at 10:27
source share



All Articles