In accordance with the initial question: “How to install JRE from Inno script?”, And as the best starting solution, I propose a solution that, I think, works more coherently.
I understand that the user wants to install the JRE for his application if the Java runtime is not installed on the target computer, or if its version is lower than required. Well, I suggest using the AfterInstall parameter and slightly changing the distribution order of the distribution files.
First, we sort the files in the [Files] section, first setting the files with the renaming setting.
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 take is to change the [Run] section as follows.
[Run] Filename: "{app}\{#MyAppExeName}"; \ Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; \ Flags: nowait postinstall skipifsilent
And last but not least, we performed the RunJavaInstaller() section in the [Code] procedure as follows:
[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 Disabled with 0 . > if the Java installer is not working properly. I have not experienced any problems with this. In any case, in the code you have a comment on the Oracle link if you want to take a look.
Finally, since, unfortunately, we cannot get the status of the JRE installation in any way, we show a message and a progress bar so that the user does not feel that the installer hanged himself. To do this, save the state earlier, execute Exec with the ewWaitUntilTerminated flag to wait for this installation to complete before continuing with ours, and we will restore the previous state after the function has finished.