6

我正在使用 Inno Setup 为我正在处理的项目制作安装程序,我需要它来设置 java 环境变量,这样当他们运行 cmd 时,他们就不会收到java not found错误或类似的错误。

我发现了一些与 Inno Setup 和设置环境变量相关的其他帖子,但是有人知道这个实例的特定内容吗?

4

2 回答 2

13

Assuming Java is installed in its default location program files/Java, something like this should work in your case:

[Registry]
; set PATH
Root: HKCU; Subkey: "Environment"; ValueType:string; ValueName:"PATH"; ValueData:"{olddata};{pf}\Java\bin"; Flags: preservestringtype
; set JAVA_HOME
Root: HKCU; Subkey: "Environment"; ValueType:string; ValueName:"JAVA_HOME"; ValueData:"{pf}\Java"; Flags: preservestringtype

[Setup]
; Tell Windows Explorer to reload the environment
ChangesEnvironment=yes

I'm not sure which environment variable you want to set -- PATH or JAVA_HOME -- so I've added them both.

Changing PATH shouldn't be necessary since the Java installer tends to add itself to the path; IIRC it copies java.exe and javaw.exe to one of the system directories.

于 2012-05-21T14:46:17.793 回答
2

加上@Joni 的答案,您可以从注册表中获取 Java 安装目录并使用脚本常量来设置您的环境变量:

(编辑:感谢@TLama 的代码更正!)

[Registry]
Root: HKCU; Subkey: "Environment"; ValueType:string; ValueName:"JAVA_HOME"; ValueData:"{code:GetJava32Path|6}"; Flags: preservestringtype

[Code]
const
  RegKeyJRE = 'SOFTWARE\JavaSoft\Java Runtime Environment\';

function GetJava32Path(MinVersion: string): string;
var
  I: Integer;
  Path: string;
  Versions: TArrayOfString;
begin
  Result := '';
  if RegGetSubkeyNames(HKLM, RegKeyJRE, Versions) then
    for I := 0 to GetArrayLength(Versions)-1 do
      if (Versions[I][2] = '.') and (Versions[I][3] >= MinVersion) and
        RegQueryStringValue(HKLM32, RegKeyJRE + Versions[I], 'JavaHome', Path) then
      begin
        Result := Path;
        Exit;
      end;
end;

(注意,我远不是 Pascal 脚本专家,代码可能会好很多,尽管现在它已被 @TLama 更正了 :))

于 2015-04-09T10:27:00.513 回答