6

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?

peak
  • 105,803
  • 17
  • 152
  • 177
Howes
  • 799
  • 1
  • 7
  • 15

2 Answers2

14

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.

Joni
  • 108,737
  • 14
  • 143
  • 193
  • 3
    Don't forget the ChangesEnvironment directive. – Deanna May 22 '12 at 15:13
  • @Deanna thanks, I didn't know about that one! Now I can remove the requirement to restart from one of our installers. – Joni May 22 '12 at 16:20
  • Sorry for the late reply, but this was the answer I was looking for and worked perfectly. – Howes Jun 01 '12 at 13:10
  • Note that there's a missing ";" right before the "Flags" section in each line. – Michael Yakobi Jan 05 '14 at 16:02
  • You can check out the installation directory from the registry and use script constants to set it (see [my answer below](http://stackoverflow.com/a/29535962/1098603)). – Matthieu Apr 09 '15 at 10:28
  • This is going to add the var only for the current user and not for the machine. You need to use Root: HKLM; Subkey: "SYSTEM\CurrentControlSet\Control\Session Manager\Environment" to add a global var. – vezenkov Jul 29 '15 at 05:55
2

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;

(N.B. I'm far from being a Pascal-script expert, the code could be a lot better, though now it was corrected by @TLama :))

Matthieu
  • 2,736
  • 4
  • 57
  • 87
  • Your code is definitely correct (could have been [`shortened`](http://pastebin.com/bttNaCEG) by removing those extra `begin..end` blocks, there might be a constant for the JRE registry key path (DRY sort of) and there could be removed one `if` by using `and` operator, and return from the function could have been `exit` rather than `break` - all minor, or cosmetic things). – TLama Apr 09 '15 at 12:23
  • @TLama thanks a lot, I have both pasted your code and learnt a few things :) – Matthieu Apr 09 '15 at 12:39