Check Java is present before installing

后端 未结 7 1420
傲寒
傲寒 2021-01-30 23:46

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

7条回答
  •  轮回少年
    2021-01-31 00:09

    There is another way now. You can include a Stub setup - online installer, which will download and install actual setup.
    The filename name as for now is JavaSetup8u121.exe, which suggests it might be version specific. I don't have an older one to test if it will download actual or specific, older version.
    And for now, it seems it only does install 32bit JRE.

    [Files]
    #define JavaInstaller "JavaSetup8u121.exe"
    Source: "include\{#JavaInstaller}"; DestDir: "{tmp}";
    
    [Code]
    const
      REQUIRED_JAVA_VERSION = '1.7';
    
    function isJavaInstalled(): Boolean;
    var
      JavaVer : String;
      tmpFileName,
      pathJavaExe: String;
      isGoodJavaVersion,
      isFoundJavaPath: Boolean;
      ResultCode: Integer;
      ExecStdout: AnsiString;
    begin
    
      { *** check in registry }
      { sets variables: }
      {   JavaVer }
      {   isGoodJavaVersion }
      if RegQueryStringValue(HKLM, 'SOFTWARE\JavaSoft\Java Runtime Environment',
               'CurrentVersion', JavaVer) AND (JavaVer <> '') OR
         RegQueryStringValue(HKLM64, 'SOFTWARE\JavaSoft\Java Runtime Environment',
               'CurrentVersion', JavaVer) AND (JavaVer <> '') then begin
        Log('* Java Entry in Registry present. Version: ' + JavaVer);
        isGoodJavaVersion := CompareStr(JavaVer, REQUIRED_JAVA_VERSION) >= 0;
      end;
    
      { add additional checks, for example by searching the PATH, }
      { or by running `java -version` }
    
      Result := isGoodJavaVersion;
    end;
    
    [Run]
    Filename: "{tmp}\{#JavaInstaller}"; Parameters: "SPONSORS=0"; \
       StatusMsg: "Java Runtime Enviroment not installed on your system. Installing..."; \
       Check: not isJavaInstalled
    

    Searches for 32 and 64 bit versions in registry, internal function CompareStr() is actually usable for comparing versions in String, you can change >= 0 to =0 if you want to check against the exact version and not 'at least'.

    Alternatively Exec() could be used instead of [Run], if you want to cancel the whole install in case when the user will not go through with the Java install by cancelling it or because of an error.

提交回复
热议问题