简体   繁体   中英

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.

Similarly, in Inno Setup we can use 'ExpandConstant({%PATH|DefaultValue})' to get path variable value list.

My requirement is : If user is using zip version of JRE so there won't be any entry in registry. So, I'll have to read the PATH variable or JAVA_HOME to get the path of 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. Is it possible? Please help.

The easiest (and even the correct) way is to find the path, where java.exe is.

You can use FileSearch function , like:

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:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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