简体   繁体   中英

Inno Setup - Setting Java Environment Variable

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

I have found a few other posts related to Inno Setup and the setting environment variables, but does anyone know something specific to this instance?

Assuming Java is installed in its default location program files/Java, 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've added them both.

Changing PATH shouldn't be necessary since the Java installer tends to add itself to the path; IIRC it copies java.exe and javaw.exe to one of the system directories.

Adding up to @Joni's answer, you can get the Java installation directory from the registry and use script constants to set your environment variable:

(EDIT: thanks to @TLama for code correction!)

[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'm far from being a Pascal-script expert, the code could be a lot better, though now it was corrected by @TLama :))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM