@NotNull
  private static Sdk getJdk(
      @Nullable Project project, MavenRunnerSettings runnerSettings, boolean isGlobalRunnerSettings)
      throws ExecutionException {
    String name = runnerSettings.getJreName();
    if (name.equals(MavenRunnerSettings.USE_INTERNAL_JAVA)) {
      return JavaAwareProjectJdkTableImpl.getInstanceEx().getInternalJdk();
    }

    if (name.equals(MavenRunnerSettings.USE_PROJECT_JDK)) {
      if (project != null) {
        Sdk res = ProjectRootManager.getInstance(project).getProjectSdk();
        if (res != null) {
          return res;
        }
      }

      if (project == null) {
        Sdk recent = ProjectJdkTable.getInstance().findMostRecentSdkOfType(JavaSdk.getInstance());
        if (recent != null) return recent;
        return JavaAwareProjectJdkTableImpl.getInstanceEx().getInternalJdk();
      }

      throw new ProjectJdkSettingsOpenerExecutionException(
          "Project JDK is not specified. <a href='#'>Configure</a>", project);
    }

    if (name.equals(MavenRunnerSettings.USE_JAVA_HOME)) {
      final String javaHome = System.getenv(JAVA_HOME);
      if (StringUtil.isEmptyOrSpaces(javaHome)) {
        throw new ExecutionException(RunnerBundle.message("maven.java.home.undefined"));
      }
      final Sdk jdk = JavaSdk.getInstance().createJdk("", javaHome);
      if (jdk == null) {
        throw new ExecutionException(RunnerBundle.message("maven.java.home.invalid", javaHome));
      }
      return jdk;
    }

    for (Sdk projectJdk : ProjectJdkTable.getInstance().getAllJdks()) {
      if (projectJdk.getName().equals(name)) {
        return projectJdk;
      }
    }

    if (isGlobalRunnerSettings) {
      throw new ExecutionException(
          RunnerBundle.message("maven.java.not.found.default.config", name));
    } else {
      throw new ExecutionException(RunnerBundle.message("maven.java.not.found", name));
    }
  }
  /**
   * @param coreSettings
   * @param project used to creation fix if maven home not found
   * @param runConfiguration used to creation fix if maven home not found
   * @return
   * @throws ExecutionException
   */
  public static String resolveMavenHome(
      @NotNull MavenGeneralSettings coreSettings,
      @Nullable Project project,
      @Nullable MavenRunConfiguration runConfiguration)
      throws ExecutionException {
    final File file = MavenUtil.resolveMavenHomeDirectory(coreSettings.getMavenHome());

    if (file == null) {
      throw createExecutionException(
          RunnerBundle.message("external.maven.home.no.default"),
          RunnerBundle.message("external.maven.home.no.default.with.fix"),
          coreSettings,
          project,
          runConfiguration);
    }

    if (!file.exists()) {
      throw createExecutionException(
          RunnerBundle.message("external.maven.home.does.not.exist", file.getPath()),
          RunnerBundle.message("external.maven.home.does.not.exist.with.fix", file.getPath()),
          coreSettings,
          project,
          runConfiguration);
    }

    if (!MavenUtil.isValidMavenHome(file)) {
      throw createExecutionException(
          RunnerBundle.message("external.maven.home.invalid", file.getPath()),
          RunnerBundle.message("external.maven.home.invalid.with.fix", file.getPath()),
          coreSettings,
          project,
          runConfiguration);
    }

    try {
      return file.getCanonicalPath();
    } catch (IOException e) {
      throw new ExecutionException(e.getMessage(), e);
    }
  }