From f72b9ff33cf55fe7abd367ae9c621d51e6826b6a Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Tue, 10 Jun 2014 15:44:13 +0400 Subject: [PATCH] alternative JRE: check jre specified, allow to run with another configured jdk --- .../execution/ui/AlternativeJREPanel.java | 25 +++++++++++++++++-- .../execution/util/JavaParametersUtil.java | 20 ++++++++++++--- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/java/execution/impl/src/com/intellij/execution/ui/AlternativeJREPanel.java b/java/execution/impl/src/com/intellij/execution/ui/AlternativeJREPanel.java index 605248782288..31d7b72df282 100644 --- a/java/execution/impl/src/com/intellij/execution/ui/AlternativeJREPanel.java +++ b/java/execution/impl/src/com/intellij/execution/ui/AlternativeJREPanel.java @@ -21,6 +21,7 @@ import com.intellij.openapi.projectRoots.ProjectJdkTable; import com.intellij.openapi.projectRoots.Sdk; import com.intellij.openapi.ui.ComponentWithBrowseButton; import com.intellij.openapi.ui.TextComponentAccessor; +import com.intellij.openapi.util.SystemInfo; import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.util.text.StringUtil; import com.intellij.ui.GuiUtils; @@ -29,10 +30,12 @@ import com.intellij.ui.PanelWithAnchor; import com.intellij.ui.TextFieldWithHistory; import com.intellij.ui.components.JBCheckBox; import net.miginfocom.swing.MigLayout; +import sun.plugin2.util.SystemUtil; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.io.File; import java.util.ArrayList; /** @@ -49,16 +52,34 @@ public class AlternativeJREPanel extends JPanel implements PanelWithAnchor { myCbEnabled = new JBCheckBox(ExecutionBundle.message("run.configuration.use.alternate.jre.checkbox")); myFieldWithHistory = new TextFieldWithHistory(); + myFieldWithHistory.setHistorySize(-1); final ArrayList foundJDKs = new ArrayList(); + final Sdk[] allJDKs = ProjectJdkTable.getInstance().getAllJdks(); + + for (Sdk sdk : allJDKs) { + foundJDKs.add(sdk.getName()); + } + for (JreProvider provider : JreProvider.EP_NAME.getExtensions()) { String path = provider.getJrePath(); if (!StringUtil.isEmpty(path)) { foundJDKs.add(path); } } - final Sdk[] allJDKs = ProjectJdkTable.getInstance().getAllJdks(); + for (Sdk jdk : allJDKs) { - foundJDKs.add(jdk.getHomePath()); + String homePath = jdk.getHomePath(); + + if (!SystemInfo.isMac) { + final File jre = new File(jdk.getHomePath(), "jre"); + if (jre.isDirectory()) { + homePath = jre.getPath(); + } + } + + if (!foundJDKs.contains(homePath)) { + foundJDKs.add(homePath); + } } myFieldWithHistory.setHistory(foundJDKs); myPathField = new ComponentWithBrowseButton(myFieldWithHistory, null); diff --git a/java/execution/impl/src/com/intellij/execution/util/JavaParametersUtil.java b/java/execution/impl/src/com/intellij/execution/util/JavaParametersUtil.java index 8abf465104dd..18aead7fe7f1 100644 --- a/java/execution/impl/src/com/intellij/execution/util/JavaParametersUtil.java +++ b/java/execution/impl/src/com/intellij/execution/util/JavaParametersUtil.java @@ -27,6 +27,8 @@ import com.intellij.openapi.module.Module; import com.intellij.openapi.module.ModuleUtilCore; import com.intellij.openapi.project.Project; import com.intellij.openapi.projectRoots.JavaSdk; +import com.intellij.openapi.projectRoots.JavaSdkType; +import com.intellij.openapi.projectRoots.ProjectJdkTable; import com.intellij.openapi.projectRoots.Sdk; import com.intellij.openapi.projectRoots.ex.PathUtilEx; import com.intellij.openapi.roots.*; @@ -36,6 +38,7 @@ import com.intellij.psi.PsiClass; import com.intellij.psi.PsiFile; import org.intellij.lang.annotations.MagicConstant; +import java.io.File; import java.util.List; import java.util.Map; @@ -128,6 +131,14 @@ public class JavaParametersUtil { } private static Sdk createAlternativeJdk(final String jreHome) throws CantRunException { + final Sdk configuredJdk = ProjectJdkTable.getInstance().findJdk(jreHome); + if (configuredJdk != null) { + return configuredJdk; + } + final boolean isJdk = JavaSdk.checkForJdk(new File(jreHome)); + if (isJdk) { + throw new CantRunException("Jre expected but jdk found"); + } final Sdk jdk = JavaSdk.getInstance().createJdk("", jreHome); if (jdk == null) throw CantRunException.noJdkConfigured(); return jdk; @@ -135,11 +146,12 @@ public class JavaParametersUtil { public static void checkAlternativeJRE(CommonJavaRunConfigurationParameters configuration) throws RuntimeConfigurationWarning { if (configuration.isAlternativeJrePathEnabled()) { - if (configuration.getAlternativeJrePath() == null || - configuration.getAlternativeJrePath().length() == 0 || - !JavaSdk.checkForJre(configuration.getAlternativeJrePath())) { + final String alternativeJrePath = configuration.getAlternativeJrePath(); + if (alternativeJrePath == null || + alternativeJrePath.length() == 0 || + ProjectJdkTable.getInstance().findJdk(alternativeJrePath) == null && !JavaSdk.checkForJre(alternativeJrePath)) { throw new RuntimeConfigurationWarning( - ExecutionBundle.message("jre.path.is.not.valid.jre.home.error.mesage", configuration.getAlternativeJrePath())); + ExecutionBundle.message("jre.path.is.not.valid.jre.home.error.mesage", alternativeJrePath)); } } }