安装前检查Java是否存在 [英] Check Java is present before installing
问题描述
我正在为jar应用创建一个Inno Setup安装程序。
我现在要做的是在继续安装之前检查java是否存在。所以我只需要确保用户能够运行:
I'm creating an Inno Setup installer for a jar app. What I want to do right now is to check if java is present before proceeding with the install. So I only need to be sure the users will be able to run:
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)
Is checking the registry enough to be sure java's home dir will be in the PATH? (to be able to run "java" in the console)
如果安装了更高版本的java,无论如何都会存在注册表中的密钥或者我将不得不检查每个更高版本?
If a higher version of java is installed, will that key in the registry exist anyway or I will have to check for each higher version possible?
有没有人有更好的方式来下载java,而不仅仅是显示弹出窗口并将用户带到下载页面?
Does anyone have a better way to download java than just showing a popup and taking the users to the download page?
推荐答案
我希望有人觉得这很有用,我所做的就是重复使用放在Inno Setups wiki中的一段代码来制作一个< >与版本作为数字进行比较:
I hope someone finds this useful, what I did is reusing some piece of the code placed in Inno Setups wiki to make a < > comparison with the version as a number:
{ 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;
感谢您的帮助
这篇关于安装前检查Java是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!