在Inno Setup中找到JRE安装路径

时间:2018-07-08 16:23:18

标签: java inno-setup

以下是我的要求:

  1. 检查是否已安装Java
  2. 检查它是否安装在自定义目录中
  3. 如果是,则将目录路径保存在变量中
  4. 否则,将检测版本并将标准路径保存在变量中

下面是检测版本并将标准路径保存到变量的代码

我的代码存在问题:

  1. 如果同时安装了32位和64位,则它会检测到这两者。我的目的是在同时安装了32位和64位的情况下,仅检测到64位。
  2. if DirExists(ExpandConstant('{pf32}\java\')) then是我可以用来检测自定义目录的吗?
  3. 我认为上面的代码不是查找Java自定义目录的正确方法。如果用户安装在Java以外的其他文件夹中。另一个问题是,如果我们卸载Java,它不会删除文件夹java / JRE。

我正在使用Need help on Inno Setup script - issue in check the jre install中的@TLama代码

[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;

2 个答案:

答案 0 :(得分:3)

JRE安装路径像这样存储在注册表中:

[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"

您可以使用以下代码检索最新版本(具有64位首选项)的安装路径:

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;

答案 1 :(得分:0)

根据您安装的 JRE,注册表数据可能不存在。更通用的解决方案可能更可取。

我想要一些可以在多个 Inno Setup 项目中使用的东西,所以我编写了一个 DLL 来检测 Java 详细信息(主目录等):

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

从这里下载:https://github.com/Bill-Stewart/JavaInfo/releases

下载内容包括一个示例 Inno Setup .iss 脚本,该脚本演示了如何使用 DLL 函数。