1

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;
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
skr
  • 1,700
  • 1
  • 15
  • 39

2 Answers2

3

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;
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • The registry data described in this answer doesn't apply to all JDK/JRE variants. Try using JavaInfo.dll instead (see my answer). – Bill_Stewart Jun 09 '21 at 21:33
1

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.

Bill_Stewart
  • 22,916
  • 4
  • 51
  • 62
  • I'm trying your solution, but it's so much that info that I almost want to give up. I'm a software engineering student and I just wrote my simple JavaFX program and simple Inno Setup. Would be nice if there was a very short example of just detecting Java and java min version and if there wasn't Java JRE or min version available install .msi silently with parameters. I'm trying to do that with your example but I get headaches already. Especially because I'm new to all this and .msi parameters. I can't make head or tails with this: https://adoptium.net/en-GB/installation/windows/ – Remzi Cavdar Oct 15 '22 at 02:45
  • There is a very short example of just detecting Java using JavaInfo.dll -- [https://github.com/Bill-Stewart/JavaInfo/blob/master/JavaInfoTest.iss](https://github.com/Bill-Stewart/JavaInfo/blob/master/JavaInfoTest.iss) – Bill_Stewart Oct 17 '22 at 14:19