Improve detecting the default Java location on MacOS (it depends on whether the version is before or since 10.5)

GitOrigin-RevId: f8c514854d25bafa7fa720c3feaac45cc3851a09
This commit is contained in:
Leonid Bushuev
2020-01-13 16:21:48 +00:00
committed by intellij-monorepo-bot
parent 92ac1f8dcc
commit 21cc81d2a9
@@ -148,6 +148,8 @@ public abstract class JavaHomeFinder {
private static class MacFinder extends DefaultFinder {
public static final String JAVA_HOME_FIND_UTIL = "/usr/libexec/java_home";
MacFinder() {
super("/Library/Java/JavaVirtualMachines", "/System/Library/Java/JavaVirtualMachines");
}
@@ -156,15 +158,44 @@ public abstract class JavaHomeFinder {
@Override
public List<String> findExistingJdks() {
List<String> list = super.findExistingJdks();
if (new File("/usr/libexec/java_home").canExecute()) {
String path = ExecUtil.execAndReadLine(new GeneralCommandLine("/usr/libexec/java_home"));
if (path != null && new File(path).isDirectory()) {
list.add(path);
}
String defaultJavaHome = getSystemDefaultJavaHome();
if (defaultJavaHome != null) {
ArrayList<String> list2 = new ArrayList<>(list.size() + 1);
list2.add(defaultJavaHome);
list2.addAll(list);
list = list2;
}
return list;
}
@Nullable
private String getSystemDefaultJavaHome() {
String homePath = null;
if (SystemInfo.isMacOSLeopard) {
// since version 10.5
if (canExecute(JAVA_HOME_FIND_UTIL)) homePath = ExecUtil.execAndReadLine(new GeneralCommandLine(JAVA_HOME_FIND_UTIL));
}
else {
// before version 10.5
homePath = "/Library/Java/Home";
}
return isDirectory(homePath) ? homePath : null;
}
private static boolean canExecute(@Nullable String filePath) {
if (filePath == null) return false;
File file = new File(filePath);
return file.canExecute();
}
private static boolean isDirectory(@Nullable String path) {
if (path == null) return false;
File dir = new File(path);
return dir.isDirectory();
}
@Override
protected File adjustPath(File file) {
File home = new File(file, "/Home");