【问题标题】:Find JRE installation path in Inno Setup在 Inno Setup 中查找 JRE 安装路径
【发布时间】:2023-03-18 08:45:01
【问题描述】:

以下是我的要求:

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

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

我的代码有问题:

  1. 如果同时安装了 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;

【问题讨论】:

    标签: java inno-setup


    【解决方案1】:

    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;
    

    【讨论】:

    • 此答案中描述的注册表数据不适用于所有 JDK/JRE 变体。尝试改用 JavaInfo.dll(请参阅我的回答)。
    【解决方案2】:

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

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多