2009-08-19 15 views
18

Sto creando un programma di installazione di Inno Setup per un'app jar. Quello che voglio fare adesso è controllare se java è presente prima di procedere con l'installazione. Così ho solo bisogno di essere sicuri gli utenti saranno in grado di eseguire:Verificare che Java sia presente prima di installare

java -jar my-app.jar 

Quello che sto facendo in questo momento è:

[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; 

Le mie domande sono:

  • Is controllando il registro abbastanza per essere sicuro che la directory home di java sarà nel PERCORSO? (per essere in grado di eseguire "java" nella console)

  • Se è installata una versione superiore di java, quella chiave del registro esiste comunque o dovrò controllare per ogni versione superiore possibile?

  • Qualcuno ha un modo migliore per scaricare java di mostrare semplicemente un popup e portare gli utenti alla pagina di download?

+2

Avete intenzione di richiedere Sun Java o avete intenzione di consentire qualsiasi versione (come BEA - erm, Oracle - JRokit)? – atk

+0

Bella domanda. Mi limiterò a Sun Java poiché il mio strumento è basato sul supporto ufficiale e non voglio avere problemi futuri a causa di diverse implementazioni java. – Santi

risposta

13

Spero che qualcuno trova questo utile, quello che ho fatto è il riutilizzo di qualche pezzo di codice posto in Inno Setup wiki per fare un <> confronto con la versione come un numero:

{ 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; 

Grazie a tutti per la vostra aiutare

+0

Il tuo script supporta anche l'installazione di JRE/JDK? Molti utenti ottengono Java e quindi non installano JDK. – YumYumYum

2

Per la terza domanda, vedere here, sotto la sezione marchi e licenze. Riepilogo Exec: puoi distribuire JRE insieme alla tua app. Penso che alcune app lo facciano per assicurare che non abbiano problemi di compatibilità delle versioni: installo JRE in una sottocartella dell'app stessa.

Per quanto riguarda il PERCORSO, perché è così importante? È possibile creare un collegamento che fa riferimento a java.exe tramite il percorso completo per eseguire l'app. È importante che l'utente avvii il programma tramite la riga di comando?

+0

Non vorrei raggruppare un intero JRE con la mia app, aggiunge troppo peso all'installatore e può non essere necessario se gli utenti lo hanno già. Il secondo suggerimento sembra buono. Sembra che posso trovare da dove ottenere la java home dir dal registro anche ... – Santi

+0

hai qualche link su come fare il percorso completo per java.exe? – rogerdpack

0

Invece di controllare per una versione specifica, è possibile utilizzare

function RegKeyExists(const RootKey: Integer; const SubKeyName: String): Boolean; 

per ottenere le sottochiavi di HKLM \ SOFTWARE \ JavaSoft \ Java Runtime Environment. (L'installazione parallela di diverse versioni è possibile? Non so ...) Dovresti eseguire alcune stringhe di stringa per verificare se è installato 1.6 o versione successiva, ma sarebbe più flessibile rispetto alla verifica di un numero di versione specifico.

5

ho cambiato il codice un po ', credo che le versioni più recenti di questo modo di Java saranno supportate ;-)

function InitializeSetup(): Boolean; 
var 
ErrorCode: Integer; 
JavaInstalled : Boolean; 
Result1 : Boolean; 
Versions: TArrayOfString; 
I: Integer; 
begin 
if RegGetSubkeyNames(HKLM, 'SOFTWARE\JavaSoft\Java Runtime Environment', Versions) then 
begin 
    for I := 0 to GetArrayLength(Versions)-1 do 
    if JavaInstalled = true then 
    begin 
    //do nothing 
    end else 
    begin 
    if (Versions[I][2]='.') and ((StrToInt(Versions[I][1]) > 1) or ((StrToInt(Versions[I][1]) = 1) and (StrToInt(Versions[I][3]) >= 6))) then 
    begin 
    JavaInstalled := true; 
    end else 
    begin 
    JavaInstalled := false; 
    end; 
    end; 
end else 
begin 
    JavaInstalled := false; 
end; 


//JavaInstalled := RegKeyExists(HKLM,'SOFTWARE\JavaSoft\Java Runtime Environment\1.9'); 
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://www.java.com/getjava/', 
    '','',SW_SHOWNORMAL,ewNoWait,ErrorCode); 
    end; 
    end; 
end; 


end. 
2

Altro miglioramento per l'alrea dy defined code:

  1. Verificare l'esistenza di JRE/JDK.
  2. Verificare in visualizzazione a 32 o 64 bit del registro.

Codice:

function InitializeSetup(): Boolean; 
var 
ErrorCode: Integer; 
JavaInstalled : Boolean; 
ResultMsg : Boolean; 
Versions: TArrayOfString; 
I: Integer; 
regRoot: Integer; 
begin 
// Check which view of registry should be taken: 
regRoot := HKLM 
begin 
    if IsWin64 then 
    begin 
    regRoot := HKLM64 
    end; 
end; 
if (RegGetSubkeyNames(regRoot, 'SOFTWARE\JavaSoft\Java Runtime Environment', Versions)) or (RegGetSubkeyNames(regRoot, 'SOFTWARE\JavaSoft\Java Development Kit', Versions)) then 
begin 
    for I := 0 to GetArrayLength(Versions)-1 do 
    if JavaInstalled = true then 
    begin 
    //do nothing 
    end else 
    begin 
    if (Versions[I][2]='.') and ((StrToInt(Versions[I][1]) > 1) or ((StrToInt(Versions[I][1]) = 1) and (StrToInt(Versions[I][3]) >= 7))) then 
    begin 
    JavaInstalled := true; 
    end else 
    begin 
    JavaInstalled := false; 
    end; 
    end; 
end else 
begin 
    JavaInstalled := false; 
end; 

if JavaInstalled then 
begin 
    Result := true; 
end else 
    begin 
    ResultMsg := MsgBox('Oracle Java v1.7 or newer not found in the system. Java 1.7 or later is required to run this application (can be installed after this installation too). Do you want to continue?', 
    mbConfirmation, MB_YESNO) = idYes; 
    if ResultMsg = false then 
    begin 
    Result := false; 
    end else 
    begin 
    Result := true; 
    ShellExec('open', 
    'http://www.java.com/getjava/', 
    '','',SW_SHOWNORMAL,ewNoWait,ErrorCode); 
    end; 
    end; 
end; 

end. 
+1

Hai aggiunto solo la vista in una radice del registro diversa, per quanto posso vedere. Inoltre, hai copiato il codice con tutti gli errori fatti lì ... – TLama

+0

Come ho già detto, ho aggiunto il check in macchine Win64 alla root del registro _HKLM64_. Ciò che non hai notato è che ora il controllo include anche il percorso _SOFTWARE \ JavaSoft \ Java Development Kit_. –

1

Una semplice alternativa alle risposte già proposti:

[Code] 
function InitializeSetup(): boolean; 
var 
    ResultCode: integer; 
begin 
    if Exec('java', '-version', '', SW_SHOW, ewWaitUntilTerminated, ResultCode) then begin 
    Result := true;  
    end 
    else begin   
    if 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 then begin 
     Result := false; 
     ShellExec('open', 'https://java.com/download/', '', '', SW_SHOWNORMAL, ewNoWait, ResultCode); 
    end; 
    end; 
end; 
+0

Verifica questo per la versione specifica di Java? Cosa succede se è stato trovato 1.5? – Snake

2

C'è un altro modo ora. È possibile includere uno Stub setup - online installer, che scaricherà e installerà la configurazione effettiva.
Il nome del nome file per ora è JavaSetup8u121.exe, il che suggerisce che potrebbe essere specifico per la versione. Non ho uno più vecchio per testare se scaricherà la versione attuale o specifica più vecchia.
E per ora sembra solo installare JRE a 32 bit.

[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 

Ricerche per 32 e 64 bit di registro, funzione interna CompareStr() in realtà è utilizzabile per confrontare le versioni in Stringa, è possibile modificare >= 0-=0 se si desidera controllare contro la versione esatta e non 'almeno '.

In alternativa è possibile utilizzare Exec() invece di [Run], se si desidera annullare l'intera installazione nel caso in cui l'utente non esegua l'installazione Java annullandola o a causa di un errore.

Problemi correlati