简体   繁体   English

如何从 Inno Setup 安装 JRE?

[英]How do I install a JRE from an Inno Setup?

I'm trying to install the most current platform (x64 or x86) appropriate Java Runtime Environment via Inno Setup (along with another application).我正在尝试通过 Inno Setup(以及另一个应用程序)安装最新的平台(x64 或 x86)合适的 Java 运行时环境。 I've found some script examples for how to detect the version and install if correct and adapted them to my needs but I keep running into this:我找到了一些关于如何检测版本和安装(如果正确)并根据我的需要调整它们的脚本示例,但我一直遇到这个问题:

Unable to open file "path\\to\\JREInstall.exe":无法打开文件“path\\to\\JREInstall.exe”:
CreateProcess failed: Code 5: CreateProcess 失败:代码 5:
Access Is Denied访问被拒绝

This is the code strictly responsible for installing the JRE:这是严格负责安装 JRE 的代码:

[Setup]
AppName="JRE Setup"
AppVersion=0.1
DefaultDirName="JRE Setup"

[Languages]
Name: "english"; MessagesFile: "compiler:Default.isl"

[Files]
Source: "jre-8u11-windows-x64.exe"; DestDir: "{tmp}\JREInstall.exe"; \
    Check: IsWin64 AND InstallJava();
Source: "jre-8u11-windows-i586.exe"; DestDir: "{tmp}\JREInstall.exe"; \
    Check: (NOT IsWin64) AND InstallJava();

[Run]
Filename: "{tmp}\JREInstall.exe"; Parameters: "/s"; \
    Flags: nowait postinstall runhidden runascurrentuser; Check: 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;

In the full setup script the same message continues to come up.在完整的安装脚本中,同样的消息继续出现。 How can I get the JRE Setup to run from this scripted setup file?我怎样才能从这个脚本化的安装文件中运行 JRE 安装程序?

I was able to figure out the issue: Evidently I was mistaken in my use of these lines:我能够找出问题所在:显然我在使用这些行时犯了错误:

Source: "jre-8u11-windows-x64.exe"; DestDir: "{tmp}\JREInstall.exe"; Check: IsWin64 AND InstallJava();
Source: "jre-8u11-windows-i586.exe"; DestDir: "{tmp}\JREInstall.exe"; Check: (NOT IsWin64) AND InstallJava();

and they should have been in place like so:他们应该像这样:

Source: "jre-8u11-windows-x64.exe"; DestDir: "{tmp}"; DestName: "JREInstall.exe"; Check: IsWin64 AND InstallJava();
Source: "jre-8u11-windows-i586.exe"; DestDir: "{tmp}"; DestName: "JREInstall.exe"; Check: (NOT IsWin64) AND InstallJava();

That seems to have solved the problem.这似乎解决了问题。

Also this line I was mistaken in:还有这一行我被误认为了:

Filename: "{tmp}\JREInstall.exe"; Parameters: "/s"; Flags: nowait postinstall runhidden runascurrentuser; Check: InstallJava()

It should have been:本来应该是:

Filename: "{tmp}\JREInstall.exe"; Parameters: "/s"; Flags: nowait runhidden runascurrentuser; Check: InstallJava()

This is the best solution my limited experience with this particular tool is able to come up with.这是我对这个特定工具的有限经验能够提出的最佳解决方案。 I will look into the PrepareToInstall option when I have a chance but this works for now.当我有机会时,我会研究 PrepareToInstall 选项,但这目前有效。

According to the initial question, "How do I install a JRE from an Inno script?", and taking as a starting solution the best proposed one, I propose a solution that I think works more coherently.根据最初的问题,“我如何从 Inno 脚本安装 JRE?”,并以最好的建议作为起始解决方案,我提出了一个我认为更一致的解决方案。

I understand that the user wants to install a JRE for their application if the target computer does not have installed a Java runtime environment or its version is lower than the required one.我了解如果目标计算机没有安装 Java 运行时环境或其版本低于所需的版本,则用户希望为其应用程序安装 JRE。 Ok, what I propose is to use the AfterInstall parameter and reorder a bit the distribution files in a different way.好的,我建议使用AfterInstall参数并以不同的方式对分发文件重新排序。

We will first sort the files in the [Files] section in another way, putting first the redist install files.我们将首先以另一种方式对[Files]部分中的文件进行排序,将 redist 安装文件放在首位。

Source: "redist\jre-8u121-windows-i586.exe"; DestDir: "{tmp}"; DestName: "JREInstaller.exe";\
    Flags: deleteafterinstall; AfterInstall: RunJavaInstaller(); \
    Check: (NOT IsWin64) AND InstallJava();
Source: "redist\jre-8u121-windows-x64.exe"; DestDir: "{tmp}"; DestName: "JREInstaller.exe"; \
    Flags: deleteafterinstall; AfterInstall: RunJavaInstaller(); \
    Check: IsWin64 AND InstallJava();
Source: "Myprog.exe"; DestDir: "{app}"; Flags: ignoreversion

The next step we must do is to modify the section [Run] as follows.我们必须做的下一步是修改[Run]部分,如下所示。

[Run]
Filename: "{app}\{#MyAppExeName}"; \
    Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; \
    Flags: nowait postinstall skipifsilent

And last but not least, we implemented in the [Code] section the RunJavaInstaller() procedure as follows:最后但并非最不重要的一点是,我们在[Code]部分实现了RunJavaInstaller()过程,如下所示:

[Code]

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;

You may need to replace the Enabled value with 1 and the Disabled value with 0 if the Java runtime installer is not working properly.如果 Java 运行时安装程序工作不正常,您可能需要将Enabled值替换为1 ,将Disabled值替换为0 I have not experienced any problem doing it this way.我这样做没有遇到任何问题。 Anyways, in the code you have a comment with the Oracle link if you want to take a look.无论如何,如果您想看一看,在代码中您可以对 Oracle 链接进行评论。

Finally, since unfortunately we can not receive the installation progress status of the JRE in any way, we show a message and a progress bar so that the user does not have the feeling that the installer has hung.最后,由于遗憾的是我们无法以任何方式接收到 JRE 的安装进度状态,因此我们显示了一条消息和一个进度条,让用户不会有安装程序挂起的感觉。 To do this, we save the state before, execute Exec with the flag ewWaitUntilTerminated , to wait for that installation to finish before continuing with ours, and we restore the previous state once the function execution has finished.为此,我们保存之前的状态,使用标志ewWaitUntilTerminated执行Exec ,等待安装完成后再继续我们的安装,一旦函数执行完成,我们恢复先前的状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM