diff --git a/java/java-impl/src/com/intellij/openapi/projectRoots/impl/JavaHomeFinder.java b/java/java-impl/src/com/intellij/openapi/projectRoots/impl/JavaHomeFinder.java index b0f809f822a8..28a4316c4e1a 100644 --- a/java/java-impl/src/com/intellij/openapi/projectRoots/impl/JavaHomeFinder.java +++ b/java/java-impl/src/com/intellij/openapi/projectRoots/impl/JavaHomeFinder.java @@ -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 findExistingJdks() { List 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 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");