描述: 我想使用innosetup安装JRE,假设在windows系统中已经安装了JRE 1.8版本,从innosetup跳过JRE安装。
问题
当已经在系统中安装了JRE,然后从innosetup卸载JRE。在这里,我不想从innosetup卸载JRE。跳过JRE的安装。
我有以下代码我不知道错误到哪里,请帮助我。
[File] Source: "C:\Users\hyva\Desktop\HIaccounts\Dependies\jre-8u131-windows-i586.exe"; DestDir: "{tmp}"; DestName: "JREInstaller.exe";Flags: deleteafterinstall; AfterInstall: RunJavaInstaller(); Check: (NOT IsWin64) AND InstallJava(); Source: "C:\Users\hyva\Desktop\HIaccounts\Dependies\jre-8u131-windows-x64.exe"; DestDir: "{tmp}"; DestName: "JREInstaller.exe"; Flags: deleteafterinstall; AfterInstall: RunJavaInstaller(); Check: IsWin64 AND InstallJava();
[Code]
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;
function InstallJava() : Boolean;
var
ErrCode: Integer;
JVer: String;
InstallJ: Boolean;
begin
RegQueryStringValue(
HKLM, 'SOFTWARE\JavaSoft\Java Runtime Environment', 'CurrentVersion', JVer);
InstallJ := true;
if Length( JVer ) > 0 then
begin
if CompareVersion(JVer, '1.8') >= 0 then
begin
InstallJ := false;
end;
end;
Result := InstallJ;
end;
procedure RunJavaInstaller();
var
StatusText: string;
ResultCode: Integer;
Path, Parameters: string;
begin
Path := '{tmp}\JREInstaller.exe';
{ http://docs.oracle.com/javase/8/docs/technotes/guides/install/config.html#table_config_file_options }
Parameters := '/s INSTALL_SILENT=Enable REBOOT=Disable SPONSORS=Disable REMOVEOUTOFDATEJRES=0';
StatusText:= WizardForm.StatusLabel.Caption;
WizardForm.StatusLabel.Caption:='Installing the java runtime environment. Wait a moment ...';
WizardForm.ProgressGauge.Style := npbstMarquee;
try
if not Exec(ExpandConstant(Path), Parameters, '', SW_SHOW, ewWaitUntilTerminated, ResultCode) then
begin
{ we inform the user we couldn't install the JRE }
MsgBox('Java runtime environment install failed with error ' + IntToStr(ResultCode) +
'. Try installing it manually and try again to install MyProg.', mbError, MB_OK);
end;
finally
WizardForm.StatusLabel.Caption := StatusText;
WizardForm.ProgressGauge.Style := npbstNormal;
end;
end;
答案 0 :(得分:1)
您可能需要两个不同版本的InstallJava
函数 - 一个用于检查HKLM32
并用于not IsWin64
,另一个用于检查HKLM64
并用于{ {1}}。
(这假设Java安装程序仅更新32位或64位注册表,具体取决于您运行的安装程序,并且不会同时更新两者。我还没有验证这一点。)
答案 1 :(得分:1)
这是基于@Miral's answer的工作代码。
[Files]
Source: "C:\Users\user\Desktop\Dependies\jre-8u131-windows-i586.exe"; \
DestDir: "{tmp}"; DestName: "JREInstaller.exe"; Flags: deleteafterinstall; \
AfterInstall: RunJavaInstaller(); Check: (NOT IsWin64) AND InstallJava();
Source: "C:\Users\user\Desktop\Dependies\jre-8u131-windows-x64.exe"; \
DestDir: "{tmp}"; DestName: "JREInstaller.exe"; Flags: deleteafterinstall; \
AfterInstall: RunJavaInstaller(); Check: IsWin64 AND InstallJava64();
[Code]
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;
function DoInstallJava(RootKey: Integer) : Boolean;
var
JVer: String;
begin
RegQueryStringValue(
RootKey, 'SOFTWARE\JavaSoft\Java Runtime Environment', 'CurrentVersion', JVer);
Result := (Length( JVer ) = 0) or (CompareVersion(JVer, '1.8') < 0);
end;
function InstallJava() : Boolean;
begin
Result := DoInstallJava(HKLM);
end;
function InstallJava64() : Boolean;
begin
Result := DoInstallJava(HKLM64);
end;
procedure RunJavaInstaller();
var
StatusText: string;
ResultCode: Integer;
Path, Parameters: string;
begin
Path := '{tmp}\JREInstaller.exe';
{ http://docs.oracle.com/javase/8/docs/technotes/guides/install/config.html#table_config_file_options }
Parameters := '/s INSTALL_SILENT=Enable REBOOT=Disable SPONSORS=Disable REMOVEOUTOFDATEJRES=1';
StatusText:= WizardForm.StatusLabel.Caption;
WizardForm.StatusLabel.Caption:='Installing the java runtime environment. Wait a moment ...';
WizardForm.ProgressGauge.Style := npbstMarquee;
try
if not Exec(ExpandConstant(Path), Parameters, '', SW_SHOW, ewWaitUntilTerminated, ResultCode) then
begin
{ we inform the user we couldn't install the JRE }
MsgBox('Java runtime environment install failed with error ' + IntToStr(ResultCode) +
'. Try installing it manually and try again to install MyProg.', mbError, MB_OK);
end;
finally
WizardForm.StatusLabel.Caption := StatusText;
WizardForm.ProgressGauge.Style := npbstNormal;
end;
end;