Java Code Examples for org.eclipse.jdt.launching.JavaRuntime#getVMInstall()

The following examples show how to use org.eclipse.jdt.launching.JavaRuntime#getVMInstall() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ImportPlatformWizard.java    From hybris-commerce-eclipse-plugin with Apache License 2.0 6 votes vote down vote up
protected void fixRuntimeEnvironment( String platformDir )
{
	IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject( "platform" );
	IJavaProject javaProject = JavaCore.create( project );
	IVMInstall javaInstall = null;
	try
	{
		if(javaProject.isOpen())
		{
		javaInstall = JavaRuntime.getVMInstall( javaProject );
	}
	}
	catch( CoreException e )
	{
		throw new IllegalStateException( e );
	}
	if( javaInstall != null )
	{
		setHeapSize( javaInstall );
	}
}
 
Example 2
Source File: ProjectCommand.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Gets the project settings.
 *
 * @param uri
 *                        Uri of the source/class file that needs to be queried.
 * @param settingKeys
 *                        the settings we want to query, for example:
 *                        ["org.eclipse.jdt.core.compiler.compliance",
 *                        "org.eclipse.jdt.core.compiler.source"].
 *                        Besides the options defined in JavaCore, the following keys can also be used:
 *                        - "org.eclipse.jdt.ls.core.vm.location": Get the location of the VM assigned to build the given Java project
 * @return A <code>Map<string, string></code> with all the setting keys and
 *         their values.
 * @throws CoreException
 * @throws URISyntaxException
 */
public static Map<String, String> getProjectSettings(String uri, List<String> settingKeys) throws CoreException, URISyntaxException {
	IJavaProject javaProject = getJavaProjectFromUri(uri);
	Map<String, String> settings = new HashMap<>();
	for (String key : settingKeys) {
		switch(key) {
			case VM_LOCATION:
				IVMInstall vmInstall = JavaRuntime.getVMInstall(javaProject);
				if (vmInstall == null) {
					continue;
				}
				File location = vmInstall.getInstallLocation();
				if (location == null) {
					continue;
				}
				settings.putIfAbsent(key, location.getAbsolutePath());
				break;
			default:
				settings.putIfAbsent(key, javaProject.getOption(key, true));
				break;
		}
	}
	return settings;
}
 
Example 3
Source File: JavaModelUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Checks if the JRE of the given project or workspace default JRE have source compliance 1.5 or
 * greater.
 *
 * @param project the project to test or <code>null</code> to test the workspace JRE
 * @return <code>true</code> if the JRE of the given project or workspace default JRE have
 *         source compliance 1.5 or greater.
 * @throws CoreException if unable to determine the project's VM install
 */
public static boolean is50OrHigherJRE(IJavaProject project) throws CoreException {
	IVMInstall vmInstall;
	if (project == null) {
		vmInstall= JavaRuntime.getDefaultVMInstall();
	} else {
		vmInstall= JavaRuntime.getVMInstall(project);
	}
	if (!(vmInstall instanceof IVMInstall2))
		return true; // assume 1.5.

	String compliance= getCompilerCompliance((IVMInstall2) vmInstall, null);
	if (compliance == null)
		return true; // assume 1.5
	return is50OrHigher(compliance);
}
 
Example 4
Source File: ResolveJavaExecutableHandler.java    From java-debug with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Resolve the java executable path from the project's java runtime.
 */
public static String resolveJavaExecutable(List<Object> arguments) throws Exception {
    try {
        String mainClass = (String) arguments.get(0);
        String projectName = (String) arguments.get(1);
        IJavaProject targetProject = null;
        if (StringUtils.isNotBlank(projectName)) {
            targetProject = JdtUtils.getJavaProject(projectName);
        } else {
            List<IJavaProject> targetProjects = ResolveClasspathsHandler.getJavaProjectFromType(mainClass);
            if (!targetProjects.isEmpty()) {
                targetProject = targetProjects.get(0);
            }
        }

        if (targetProject == null) {
            return null;
        }

        IVMInstall vmInstall = JavaRuntime.getVMInstall(targetProject);
        if (vmInstall == null || vmInstall.getInstallLocation() == null) {
            return null;
        }

        File exe = findJavaExecutable(vmInstall.getInstallLocation());
        if (exe == null) {
            return null;
        }

        return exe.getAbsolutePath();
    } catch (CoreException e) {
        logger.log(Level.SEVERE, "Failed to resolve java executable: " + e.getMessage(), e);
    }

    return null;
}
 
Example 5
Source File: StandardDeployCommandHandler.java    From google-cloud-eclipse with Apache License 2.0 5 votes vote down vote up
protected boolean checkJspConfiguration(Shell shell, IProject project) throws CoreException {
  // If we have JSPs, ensure the project is configured with a JDK: required by staging
  // which precompiles the JSPs.  We could try to find a compatible JDK, but there's
  // a possibility that we select an inappropriate one and introduce problems.
  if (!WebProjectUtil.hasJsps(project)) {
    return true;
  }

  IJavaProject javaProject = JavaCore.create(project);
  IVMInstall vmInstall = JavaRuntime.getVMInstall(javaProject);
  if (JreDetector.isDevelopmentKit(vmInstall)) {
    return true;
  }

  String title = Messages.getString("vm.is.jre.title");
  String message =
      Messages.getString(
          "vm.is.jre.proceed",
          project.getName(),
          describeVm(vmInstall),
          vmInstall.getInstallLocation());
  String[] buttonLabels =
      new String[] {Messages.getString("deploy.button"), IDialogConstants.CANCEL_LABEL};
  MessageDialog dialog =
      new MessageDialog(shell, title, null, message, MessageDialog.QUESTION, 0, buttonLabels);
  return dialog.open() == 0;
}
 
Example 6
Source File: StandardDeployCommandHandler.java    From google-cloud-eclipse with Apache License 2.0 5 votes vote down vote up
@Override
protected StagingDelegate getStagingDelegate(IProject project) {
  Path javaHome = null;
  try {
    IJavaProject javaProject = JavaCore.create(project);
    IVMInstall vmInstall = JavaRuntime.getVMInstall(javaProject);
    if (vmInstall != null) {
      javaHome = vmInstall.getInstallLocation().toPath();
    }
  } catch (CoreException ex) {
    // Give up.
  }
  return new StandardStagingDelegate(project, javaHome);
}
 
Example 7
Source File: ProjectCommandTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testGetProjectVMInstallation() throws Exception {
    importProjects("maven/salut2");
    IProject project = WorkspaceHelper.getProject("salut2");
    String uriString = project.getFile("src/main/java/foo/Bar.java").getLocationURI().toString();
    List<String> settingKeys = Arrays.asList(ProjectCommand.VM_LOCATION);
    Map<String, String> options = ProjectCommand.getProjectSettings(uriString, settingKeys);

    IJavaProject javaProject = ProjectCommand.getJavaProjectFromUri(uriString);
    IVMInstall vmInstall = JavaRuntime.getVMInstall(javaProject);
    assertNotNull(vmInstall);
    File location = vmInstall.getInstallLocation();
    assertNotNull(location);
    assertEquals(location.getAbsolutePath(), options.get(ProjectCommand.VM_LOCATION));
}
 
Example 8
Source File: ProcessUtilities.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Computes the fully qualified path to the java executable for the JRE/JDK used by this project.
 *
 * @param javaProject
 * @return the path to the JRE/JDK java (executable) used on this project
 * @throws CoreException
 */
public static String computeJavaExecutableFullyQualifiedPath(IJavaProject javaProject) throws CoreException {
  IVMInstall projectVMInstall = JavaRuntime.getVMInstall(javaProject);

  if (projectVMInstall == null) {
    throw new CoreException(new Status(Status.ERROR, CorePlugin.PLUGIN_ID, "Unable to locate the JVM for project "
        + javaProject.getElementName()
        + ". Please verify that you have a project-level JVM installed by inspecting your project's build path."));
  }

  return getJavaExecutableForVMInstall(projectVMInstall);
}
 
Example 9
Source File: ReorgCorrectionsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public Object getAdditionalProposalInfo(IProgressMonitor monitor) {
	StringBuffer message= new StringBuffer();
	if (fChangeOnWorkspace) {
		message.append(Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_required_compliance_changeworkspace_description, fRequiredVersion));
	} else {
		message.append(Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_required_compliance_changeproject_description, fRequiredVersion));
	}

	try {
		IVMInstall install= JavaRuntime.getVMInstall(fProject); // can be null
		if (fChangeOnWorkspace) {
			IVMInstall vmInstall= findRequiredOrGreaterVMInstall();
			if (vmInstall != null) {
				IVMInstall defaultVM= JavaRuntime.getDefaultVMInstall(); // can be null
				if (defaultVM != null && !defaultVM.equals(install)) {
					message.append(CorrectionMessages.ReorgCorrectionsSubProcessor_50_compliance_changeProjectJREToDefault_description);
				}
				if (defaultVM == null || !isRequiredOrGreaterVMInstall(defaultVM)) {
					message.append(Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_50_compliance_changeWorkspaceJRE_description, vmInstall.getName()));
				}
			}
		} else {
			IExecutionEnvironment bestEE= findBestMatchingEE();
			if (bestEE != null) {
				if (install == null || !isEEOnClasspath(bestEE)) {
					message.append(Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_50_compliance_changeProjectJRE_description, bestEE.getId()));
				}
			}
		}
	} catch (CoreException e) {
		// ignore
	}
	return message.toString();
}
 
Example 10
Source File: ComplianceConfigurationBlock.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void validateComplianceStatus() {
		if (fJRE50InfoText != null && !fJRE50InfoText.isDisposed()) {
			boolean isVisible= false;
			String compliance= getStoredValue(PREF_COMPLIANCE); // get actual value
			IVMInstall install= null;
			if (fProject != null) { // project specific settings: only test if a 50 JRE is installed
				try {
					install= JavaRuntime.getVMInstall(JavaCore.create(fProject));
				} catch (CoreException e) {
					JavaPlugin.log(e);
				}
			} else {
				install= JavaRuntime.getDefaultVMInstall();
			}
			if (install instanceof IVMInstall2) {
				String compilerCompliance= JavaModelUtil.getCompilerCompliance((IVMInstall2) install, compliance);
				if (!compilerCompliance.equals(compliance)) { // Discourage using compiler with version other than compliance
					String[] args= { getVersionLabel(compliance), getVersionLabel(compilerCompliance) };
					if (fProject == null) {
						fJRE50InfoText.setText(Messages.format(PreferencesMessages.ComplianceConfigurationBlock_jrecompliance_info, args));
					} else {
						fJRE50InfoText.setText(Messages.format(PreferencesMessages.ComplianceConfigurationBlock_jrecompliance_info_project, args));
					}
					isVisible= true;
				}
			}
			
//			String source= getValue(PREF_SOURCE_COMPATIBILITY);
//			if (VERSION_1_8.equals(source)) {
//				fJRE50InfoText.setText("This is an implementation of an early-draft specification developed under the Java Community Process (JCP) and is made available for testing and evaluation purposes only. The code is not compatible with any specification of the JCP."); //$NON-NLS-1$
//				isVisible= true;
//			}
			
			fJRE50InfoText.setVisible(isVisible);
			fJRE50InfoImage.setImage(isVisible ? JFaceResources.getImage(Dialog.DLG_IMG_MESSAGE_WARNING) : null);
			fJRE50InfoImage.getParent().layout();
		}
	}