在Inno Setup中查找JRE安装路径



以下是我的要求:

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

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

的代码我的代码问题:

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

我正在使用 Inno 安装程序脚本需要帮助中的@TLama代码 - 检查 jre 安装时出现问题

[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, 'SoftwareJavaSoftJava 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, 'SoftwareJavaSoftJava Runtime Environment',
'CurrentVersion', JREVersion);
MsgBox('JAVA 64 bit detected.', mbInformation, MB_OK);
JREPath := 'C:Program FilesJava'
{ 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;

JRE 安装路径存储在注册表中,如下所示:

[HKEY_LOCAL_MACHINESOFTWAREJavaSoftJava Runtime Environment]
"CurrentVersion"="1.8"
[HKEY_LOCAL_MACHINESOFTWAREJavaSoftJava Runtime Environment1.8]
"JavaHome"="C:\Program Files\Java\jre1.8.0_172"

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

const
JavaKey = 'SOFTWAREJavaSoftJava 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;

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

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

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

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

下载内容包括一个示例 Inno 安装程序.iss脚本,用于演示如何使用 DLL 函数。

最新更新