skr
skr

Reputation: 1811

Find JRE installation path in Inno Setup

Followings are my requirement:

  1. Check if the Java is installed
  2. Check if it's installed in a custom directory
  3. if it's, then save the directory path in a variable
  4. Otherwise detect the version and save the standard path in a variable

Below is the code that detects the version and save the standard path to a variable

Problems with my code:

  1. If both 32 and 64 bit is installed it detects the both ..My aim is to detect only 64 bit in case both is installed.
  2. if DirExists(ExpandConstant('{pf32}\java\')) then Is this what i can use to detect custom directory?
  3. I don't think the above code is the right way to find custom directory of java. if the user installed in a different folder other than Java. the other problem is if we uninstall java it doesn't delete the folder java/JRE.

I'm using @TLama's code from Need help on Inno Setup script - issue in check the jre install

[Code]
#define MinJRE "1.7.0"
#define WebJRE "http://www.oracle.com/technetwork/java/javase/downloads/jre6downloads-1902815.html"

function IsJREInstalled: Boolean;
var
  JREVersion: string;
  JREPath:string
begin
  { read JRE version }
  Result := RegQueryStringValue(HKLM32, 'Software\JavaSoft\Java Runtime Environment',
    'CurrentVersion', JREVersion);
  MsgBox('JAVA 32 bit detected.', mbInformation, MB_OK);
  JREPath := 'C:\Program Files (x86)\Java'
  { if the previous reading failed and we're on 64-bit Windows, try to read }
  { the JRE version from WOW node }
  if not Result and IsWin64 then
    Result := RegQueryStringValue(HKLM64, 'Software\JavaSoft\Java Runtime Environment',
      'CurrentVersion', JREVersion);
  MsgBox('JAVA 64 bit detected.', mbInformation, MB_OK);
  JREPath := 'C:\Program Files\Java'
  { if the JRE version was read, check if it's at least the minimum one }
  if Result then
    Result := CompareStr(JREVersion, '{#MinJRE}') >= 0;
end;


function InitializeSetup: Boolean;
var
  ErrorCode: Integer;
begin
  Result := True;
  { check if JRE is installed; if not, then... }
  if not IsJREInstalled then
  begin
    { show a message box and let user to choose if they want to download JRE; }
    { if so, go to its download site and exit setup; continue otherwise }
    if MsgBox('Java is required. Do you want to download it now ?',
      mbConfirmation, MB_YESNO) = IDYES then
    begin
      Result := False;
      ShellExec('', '{#WebJRE}', '', '', SW_SHOWNORMAL, ewNoWait, ErrorCode);
    end;
  end;
end;

Upvotes: 1

Views: 972

Answers (2)

Bill_Stewart
Bill_Stewart

Reputation: 24585

Depending on which JRE you have installed, that registry data may not be there. A more generic solution is probably preferable.

I wanted something I could use across multiple Inno Setup projects, so I wrote a DLL for detecting Java details (home directory, etc.):

https://github.com/Bill-Stewart/JavaInfo

Download from here: https://github.com/Bill-Stewart/JavaInfo/releases

The download includes a sample Inno Setup .iss script that demonstrates how to use the DLL functions.

Upvotes: 1

Martin Prikryl
Martin Prikryl

Reputation: 202514

JRE installation path is stored in registry like this:

[HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment]
"CurrentVersion"="1.8"

[HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment\1.8]
"JavaHome"="C:\\Program Files\\Java\\jre1.8.0_172"

You can retrieve the installation path of the latest version (with 64-bit preference) using a code like this:

const
  JavaKey = 'SOFTWARE\JavaSoft\Java Runtime Environment';

function GetJavaVersionAndPath(
   RootKey: Integer; var JavaVersion: string; var JavaPath: string): Boolean;
var
  JREVersion: string;
begin
  Result :=
    RegQueryStringValue(RootKey, JavaKey, 'CurrentVersion', JavaVersion) and
    RegQueryStringValue(RootKey, JavaKey + '\' + JavaVersion, 'JavaHome', JavaPath);
end;

{ ... }
var
  JavaVersion: string;
  JavaPath: string;
begin
  if GetJavaVersionAndPath(HKLM64, JavaVersion, JavaPath) then
  begin
    Log(Format('Java %s 64-bit found in "%s"', [JavaVersion, JavaPath]));
  end
    else
  if GetJavaVersionAndPath(HKLM32, JavaVersion, JavaPath) then
  begin
    Log(Format('Java %s 32-bit found in "%s"', [JavaVersion, JavaPath]));
  end
    else
  begin
     Log('No Java found');
  end;
end;

Upvotes: 3

Related Questions