I found another, more general solution that I use in Powershell. The problem is that Java now uses symbolic links to java, javaw and javac, so you cannot always rely on the use of "where.exe java" because it returns a symbolic link.
Now I rely on Java to tell where it really works, using verbose mode and parsing the output.
$javapath=((java -verbose -version | ? {$_ -match "Opened" }).replace("[Opened ","")).replace("\lib\rt.jar]","")
It will find the path that java actually reports about this and returns the installation directory. The only problem that I did not quite understand is that it displays additional information due to the "-version" option, but the only option is the help, which is worse. However, when launched from a script, console output can simply be ignored. If anyone has a way to stay calm, I'd love to hear it.
B white
source share