gpt4 book ai didi

inno-setup - 在安装之前检查 Java 是否存在

转载 作者:IT老高 更新时间:2023-10-28 20:59:51 28 4
gpt4 key购买 nike

我正在为 jar 应用程序创建 Inno Setup 安装程序。我现在要做的是在继续安装之前检查 java 是否存在。所以我只需要确保用户能够运行:

java -jar my-app.jar

我现在正在做的是:

[Code]

function InitializeSetup(): Boolean;
var
ErrorCode: Integer;
JavaInstalled : Boolean;
Result1 : Boolean;
begin
JavaInstalled := RegKeyExists(HKLM,'SOFTWARE\JavaSoft\Java Runtime Environment\1.6');
if JavaInstalled then
begin
Result := true;
end else
begin
Result1 := MsgBox('This tool requires Java Runtime Environment version 1.6 or newer to run. Please download and install the JRE and run this setup again. Do you want to download it now?',
mbConfirmation, MB_YESNO) = idYes;
if Result1 = false then
begin
Result:=false;
end else
begin
Result:=false;
ShellExec('open',
'http://javadl.sun.com/webapps/download/AutoDL?BundleId=33787',
'','',SW_SHOWNORMAL,ewNoWait,ErrorCode);
end;
end;
end;
end;

我的问题是:

  • 检查注册表是否足以确保 java 的主目录位于 PATH 中? (能够在控制台中运行“java”)

  • 如果安装了更高版本的 java,注册表中的那个键是否仍然存在,或者我必须检查每个可能的更高版本?

  • 除了显示一个弹出窗口并将用户带到下载页面之外,还有没有更好的方法来下载 java?

最佳答案

我希望有人觉得这很有用,我所做的是重用 Inno Setups wiki 中的一些代码来与版本进行 < > 比较:

{ Both DecodeVersion and CompareVersion functions where taken from the  wiki }
procedure DecodeVersion (verstr: String; var verint: array of Integer);
var
i,p: Integer; s: string;
begin
{ initialize array }
verint := [0,0,0,0];
i := 0;
while ((Length(verstr) > 0) and (i < 4)) do
begin
p := pos ('.', verstr);
if p > 0 then
begin
if p = 1 then s:= '0' else s:= Copy (verstr, 1, p - 1);
verint[i] := StrToInt(s);
i := i + 1;
verstr := Copy (verstr, p+1, Length(verstr));
end
else
begin
verint[i] := StrToInt (verstr);
verstr := '';
end;
end;

end;

function CompareVersion (ver1, ver2: String) : Integer;
var
verint1, verint2: array of Integer;
i: integer;
begin

SetArrayLength (verint1, 4);
DecodeVersion (ver1, verint1);

SetArrayLength (verint2, 4);
DecodeVersion (ver2, verint2);

Result := 0; i := 0;
while ((Result = 0) and ( i < 4 )) do
begin
if verint1[i] > verint2[i] then
Result := 1
else
if verint1[i] < verint2[i] then
Result := -1
else
Result := 0;
i := i + 1;
end;

end;

{ Here's my code }
function InitializeSetup(): Boolean;
var
ErrorCode: Integer;
JavaVer : String;
Result1 : Boolean;
begin
RegQueryStringValue(HKLM, 'SOFTWARE\JavaSoft\Java Runtime Environment', 'CurrentVersion', JavaVer);
Result := false;
if Length( JavaVer ) > 0 then
begin
if CompareVersion(JavaVer,'1.6') >= 0 then
begin
Result := true;
end;
end;
if Result = false then
begin
Result1 := MsgBox('This tool requires Java Runtime Environment v1.6 or older to run. Please download and install JRE and run this setup again.' + #13 + #10 + 'Do you want to download it now?',
mbConfirmation, MB_YESNO) = idYes;
if Result1 = true then
begin
ShellExec('open',
'http://www.java.com/en/download/manual.jsp#win',
'','',SW_SHOWNORMAL,ewNoWait,ErrorCode);
end;
end;
end;

感谢大家的帮助

关于inno-setup - 在安装之前检查 Java 是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1297773/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com