简体   繁体   English

如何在 Inno Setup 的 PATH 中查找应用程序(Java)

[英]How to find application (Java) in PATH in Inno Setup

I can use cmd commands like set PATH to return value of PATH environment or set JAVA to return JAVA_HOME path variable value.我可以使用像set PATH这样的cmd 命令来返回PATH环境的值或set JAVA来返回JAVA_HOME路径变量值。

Similarly, in Inno Setup we can use 'ExpandConstant({%PATH|DefaultValue})' to get path variable value list.同样,在Inno Setup 中,我们可以使用'ExpandConstant({%PATH|DefaultValue})'来获取路径变量值列表。

My requirement is : If user is using zip version of JRE so there won't be any entry in registry.我的要求是:如果用户使用的是JRE zip 版本,那么注册表中不会有任何条目。 So, I'll have to read the PATH variable or JAVA_HOME to get the path of Java.因此,我必须读取PATH变量JAVA_HOME以获取 Java 的路径。

Problem : Getting the value from JAVA_HOME is quite easy but I want to extract specific path from the list of path values, for ex: if user is not using JAVA_HOME and instead using the complete path in path variable like : PATH=c:\\program files\\jre\\bin , I want to extract only this JRE path instead of entire list.问题:JAVA_HOME获取值非常容易,但我想从path值列表中提取特定路径,例如:如果用户没有使用JAVA_HOME而是使用路径变量中的完整路径,如: PATH=c:\\program files\\jre\\bin ,我只想提取这个 JRE 路径而不是整个列表。 Is it possible?是否可以? Please help.请帮忙。

The easiest (and even the correct) way is to find the path, where java.exe is.最简单(甚至正确)的方法是找到java.exe所在的路径。

You can use FileSearch function , like:您可以使用FileSearch功能,例如:

var
  Path: string;
begin
  Path := FileSearch('java.exe', GetEnv('PATH'));
  if Path = '' then
  begin
    Log('Java not found in PATH');
  end
    else
  begin
    Path := ExtractFileDir(Path);
    Log(Format('Java is in "%s"', [Path]));
  end;
end;

If you still want to take the way of looking for a path that contains JRE , you can use a code like this:如果你仍然想采取寻找包含JRE的路径的方式,你可以使用这样的代码:

var
  Path: string;
  JavaPath: string;
  S: string;
  P: Integer;
begin
  Path := GetEnv('PATH');
  while (Path <> '') and (JavaPath = '') do
  begin
    P := Pos(';', Path);
    if P = 0 then
    begin
      S := Trim(Path);
      Path := '';
    end
      else
    begin
      S := Trim(Copy(Path, 1, P - 1));
      Path := Trim(Copy(Path, P + 1, Length(Path) - P)); 
    end;

    if Pos('JDK', Uppercase(S)) > 0 then
    begin
      JavaPath := S;
    end;
  end;

  if JavaPath = '' then
  begin
    Log('Java not found in PATH');
  end
    else
  begin
    Log(Format('Java is in "%s"', [JavaPath]));
  end;
end;

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

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