From 38b1c845dbf8af4059b615caba12400d1e50c6d2 Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Wed, 8 Apr 2015 19:38:41 +0200 Subject: [PATCH] java: correct SDK locations on OS X; universal fallback --- .../projectRoots/impl/JavaSdkImpl.java | 41 ++++++++++++------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/java/java-impl/src/com/intellij/openapi/projectRoots/impl/JavaSdkImpl.java b/java/java-impl/src/com/intellij/openapi/projectRoots/impl/JavaSdkImpl.java index 4ee28ed0cbe3..fc35e56c78b2 100644 --- a/java/java-impl/src/com/intellij/openapi/projectRoots/impl/JavaSdkImpl.java +++ b/java/java-impl/src/com/intellij/openapi/projectRoots/impl/JavaSdkImpl.java @@ -144,36 +144,36 @@ public class JavaSdkImpl extends JavaSdk { } @Override - @SuppressWarnings({"HardCodedStringLiteral"}) public String suggestHomePath() { if (SystemInfo.isMac) { if (new File("/usr/libexec/java_home").canExecute()) { String path = ExecUtil.execAndReadLine(new GeneralCommandLine("/usr/libexec/java_home")); - if (path != null && new File(path).exists()) { + if (path != null && new File(path).isDirectory()) { return path; } } - return "/System/Library/Frameworks/JavaVM.framework/Versions"; + + String home = checkKnownLocations("/Library/Java/JavaVirtualMachines", "/System/Library/Java/JavaVirtualMachines"); + if (home != null) return home; } if (SystemInfo.isLinux) { - final String[] homes = {"/usr/java", "/opt/java", "/usr/lib/jvm"}; - for (String home : homes) { - if (new File(home).isDirectory()) { - return home; - } - } + String home = checkKnownLocations("/usr/java", "/opt/java", "/usr/lib/jvm"); + if (home != null) return home; } if (SystemInfo.isSolaris) { - return "/usr/jdk"; + String home = checkKnownLocations("/usr/jdk"); + if (home != null) return home; } - if (SystemInfo.isWindows) { - String property = System.getProperty("java.home"); - if (property == null) return null; - File javaHome = new File(property).getParentFile();//actually java.home points to to jre home - if (javaHome != null && JdkUtil.checkForJdk(javaHome)) { + String property = System.getProperty("java.home"); + if (property != null) { + File javaHome = new File(property); + if (javaHome.getName().equals("jre")) { + javaHome = javaHome.getParentFile(); + } + if (javaHome != null && javaHome.isDirectory()) { return javaHome.getAbsolutePath(); } } @@ -181,6 +181,17 @@ public class JavaSdkImpl extends JavaSdk { return null; } + @Nullable + private static String checkKnownLocations(String... locations) { + for (String home : locations) { + if (new File(home).isDirectory()) { + return home; + } + } + + return null; + } + @NotNull @Override public Collection suggestHomePaths() {