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

The following examples show how to use org.eclipse.jdt.launching.JavaRuntime#getDefaultVMInstall() . 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: 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 2
Source File: GradleProjectImporterTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testJavaHome() throws Exception {
	Preferences prefs = JavaLanguageServerPlugin.getPreferencesManager().getPreferences();
	String javaHomePreference = prefs.getJavaHome();
	IVMInstall defaultVM = JavaRuntime.getDefaultVMInstall();
	try {
		IVMInstallType installType = JavaRuntime.getVMInstallType(StandardVMType.ID_STANDARD_VM_TYPE);
		IVMInstall[] vms = installType.getVMInstalls();
		IVMInstall vm = vms[0];
		JavaRuntime.setDefaultVMInstall(vm, new NullProgressMonitor());
		String javaHome = new File(TestVMType.getFakeJDKsLocation(), "11").getAbsolutePath();
		prefs.setJavaHome(javaHome);
		IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
		IPath rootFolder = root.getLocation().append("/projects/gradle/simple-gradle");
		BuildConfiguration build = GradleProjectImporter.getBuildConfiguration(rootFolder.toFile().toPath());
		assertEquals(vm.getInstallLocation().getAbsolutePath(), build.getJavaHome().get().getAbsolutePath());
	} finally {
		prefs.setJavaHome(javaHomePreference);
		if (defaultVM != null) {
			JavaRuntime.setDefaultVMInstall(defaultVM, new NullProgressMonitor());
		}
	}
}
 
Example 3
Source File: WebAppProjectCreator.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Recursively find the .java files in the output directory and reformat them.
 *
 * @throws CoreException
 */
private static void reformatJavaFiles(File outDir) throws CoreException {
  // If a default JRE has not yet been detected (e.g. brand new workspace),
  // the compiler source compliance may be set to 1.3 (the default value from
  // code). This is a problem because the generated files do not compile
  // against 1.3 (e.g. they use annotations), and thus the formatting will not
  // succeed. We work around this using a trick from the
  // CompliancePreferencePage: Ensure there is a default JRE which in
  // turn updates the compliance level.
  JavaRuntime.getDefaultVMInstall();

  List<File> javaFiles = ProjectResources.findFilesInDir(outDir, javaSourceFilter);

  for (File file : javaFiles) {
    ProjectResources.reformatJavaSource(file);
  }
}
 
Example 4
Source File: AbstractJob.java    From tlaplus with MIT License 6 votes vote down vote up
protected IVMInstall getVMInstall() {
       IVMInstall vmInstall = null;

	// Try using the very same VM the Toolbox is running with (e.g.
	// this avoids problems when the Toolbox runs with a 64bit VM, but
	// the nested VM is a 32bit one).
       final String javaHome = System.getProperty("java.home");
       if (javaHome != null) {
           final IVMInstallType installType = new StandardVMType();
           vmInstall = installType.createVMInstall("TLCModelCheckerNestedVM");
           vmInstall.setInstallLocation(new File(javaHome));
           return vmInstall;
       }

       // get OS default VM (determined by path) not necessarily the same
	// the toolbox is running with.
       return JavaRuntime.getDefaultVMInstall();
}
 
Example 5
Source File: RuntimeUtils.java    From JReFrameworker with MIT License 5 votes vote down vote up
public static File getRuntimeJar(String jarName) throws IOException {
	IVMInstall vmInstall = JavaRuntime.getDefaultVMInstall();
	LibraryLocation[] locations = JavaRuntime.getLibraryLocations(vmInstall);
	for (LibraryLocation library : locations) {
		File runtime = JavaCore.newLibraryEntry(library.getSystemLibraryPath(), null, null).getPath().toFile().getCanonicalFile();
		if(runtime.getName().equals(jarName)){
			return runtime;
		}
	}
	return null;
}
 
Example 6
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();
		}
	}
 
Example 7
Source File: UIDesignerServerManager.java    From bonita-studio with GNU General Public License v2.0 5 votes vote down vote up
protected String javaBinaryLocation() throws FileNotFoundException {
    IVMInstall defaultVMInstall = JavaRuntime.getDefaultVMInstall();
    if (defaultVMInstall == null) {
        throw new FileNotFoundException("Default VM not installed");
    }
    File javaBinaryPath = StandardVMType.findJavaExecutable(defaultVMInstall.getInstallLocation());
    if (javaBinaryPath == null) {
        throw new FileNotFoundException("Java binary not configured");
    } else if (!javaBinaryPath.exists()) {
        throw new FileNotFoundException(
                String.format("Java binary not found at '%s'", javaBinaryPath.getAbsolutePath()));
    }
    return javaBinaryPath.getAbsolutePath();
}
 
Example 8
Source File: NewJavaProjectWizardPageOne.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private String getDefaultJVMName() {
	IVMInstall install= JavaRuntime.getDefaultVMInstall();
	if (install != null) {
		return install.getName();
	} else {
		return NewWizardMessages.NewJavaProjectWizardPageOne_UnknownDefaultJRE_name;
	}
}
 
Example 9
Source File: RuntimeUtils.java    From JReFrameworker with MIT License 5 votes vote down vote up
public static boolean isRuntimeJar(File jar) throws IOException {
	IVMInstall vmInstall = JavaRuntime.getDefaultVMInstall();
	LibraryLocation[] locations = JavaRuntime.getLibraryLocations(vmInstall);
	for (LibraryLocation library : locations) {
		File runtime = JavaCore.newLibraryEntry(library.getSystemLibraryPath(), null, null).getPath().toFile().getCanonicalFile();
		if(runtime.equals(jar.getCanonicalFile())){
			return true;
		}
	}
	return false;
}
 
Example 10
Source File: MainProjectWizardPage.java    From sarl with Apache License 2.0 5 votes vote down vote up
private String getDefaultJVMName() {
	final IVMInstall install = JavaRuntime.getDefaultVMInstall();
	if (install != null) {
		return install.getName();
	}
	return NewWizardMessages.NewJavaProjectWizardPageOne_UnknownDefaultJRE_name;
}
 
Example 11
Source File: MainProjectWizardPage.java    From sarl with Apache License 2.0 5 votes vote down vote up
private String getDefaultEEName() {
	final IVMInstall defaultVM = JavaRuntime.getDefaultVMInstall();

	final IExecutionEnvironment[] environments = JavaRuntime.getExecutionEnvironmentsManager()
			.getExecutionEnvironments();
	if (defaultVM != null) {
		for (int i = 0; i < environments.length; i++) {
			final IVMInstall eeDefaultVM = environments[i].getDefaultVM();
			if (eeDefaultVM != null && defaultVM.getId().equals(eeDefaultVM.getId())) {
				return environments[i].getId();
			}
		}
	}

	final String defaultCC;
	if (defaultVM instanceof IVMInstall2) {
		defaultCC = JavaModelUtil.getCompilerCompliance((IVMInstall2) defaultVM, SARLVersion.MINIMAL_JDK_VERSION_IN_SARL_PROJECT_CLASSPATH);
	} else {
		defaultCC = SARLVersion.MINIMAL_JDK_VERSION_IN_SARL_PROJECT_CLASSPATH;
	}

	for (int i = 0; i < environments.length; i++) {
		final String eeCompliance = JavaModelUtil.getExecutionEnvironmentCompliance(environments[i]);
		if (defaultCC.endsWith(eeCompliance)) {
			return environments[i].getId();
		}
	}

	return SARLVersion.MINIMAL_JDK_VERSION_IN_SARL_PROJECT_CLASSPATH;
}
 
Example 12
Source File: ReportLauncherUtils.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
public static IVMInstall getVMInstall( String name )
{
	if ( name != null )
	{
		IVMInstall[] installs = getAllVMInstances( );
		for ( int i = 0; i < installs.length; i++ )
		{
			if ( installs[i].getName( ).equals( name ) )
				return installs[i];
		}
	}
	return JavaRuntime.getDefaultVMInstall( );
}
 
Example 13
Source File: JVMConfigurator.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public static boolean configureDefaultVM(String javaHome) throws CoreException {
	if (StringUtils.isBlank(javaHome)) {
		return false;
	}
	File jvmHome = new File(javaHome);
	if (jvmHome.isDirectory()) {
		IVMInstall defaultVM = JavaRuntime.getDefaultVMInstall();
		if (defaultVM != null && jvmHome.equals(defaultVM.getInstallLocation())) {
			return false;
		}
	} else {
		JavaLanguageServerPlugin.logInfo("java.home " + jvmHome + " is not a directory");
		return false;
	}

	IVMInstall vm = findVM(jvmHome, null);
	if (vm == null) {
		IVMInstallType installType = JavaRuntime.getVMInstallType(StandardVMType.ID_STANDARD_VM_TYPE);
		long unique = System.currentTimeMillis();
		while (installType.findVMInstall(String.valueOf(unique)) != null) {
			unique++;
		}
		String vmId = String.valueOf(unique);
		VMStandin vmStandin = new VMStandin(installType, vmId);
		String name = StringUtils.defaultIfBlank(jvmHome.getName(), "JRE");
		vmStandin.setName(name);
		vmStandin.setInstallLocation(jvmHome);
		vm = vmStandin.convertToRealVM();
	}
	JavaLanguageServerPlugin.logInfo("Setting java.home " + jvmHome + " as default global VM");
	JavaRuntime.setDefaultVMInstall(vm, new NullProgressMonitor());
	JDTUtils.setCompatibleVMs(vm.getId());

	return true;
}
 
Example 14
Source File: ComplianceConfigurationBlock.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Sets the default compiler compliance options based on the current default JRE in the
 * workspace.
 * 
 * @since 3.5
 */
private void setDefaultCompilerComplianceValues() {
	IVMInstall defaultVMInstall= JavaRuntime.getDefaultVMInstall();
	if (defaultVMInstall instanceof IVMInstall2 && isOriginalDefaultCompliance()) {
		String complianceLevel= JavaModelUtil.getCompilerCompliance((IVMInstall2)defaultVMInstall, JavaCore.VERSION_1_4);
		Map<String, String> complianceOptions= new HashMap<String, String>();
		JavaModelUtil.setComplianceOptions(complianceOptions, complianceLevel);
		setDefaultValue(PREF_COMPLIANCE, complianceOptions.get(PREF_COMPLIANCE.getName()));
		setDefaultValue(PREF_PB_ASSERT_AS_IDENTIFIER, complianceOptions.get(PREF_PB_ASSERT_AS_IDENTIFIER.getName()));
		setDefaultValue(PREF_PB_ENUM_AS_IDENTIFIER, complianceOptions.get(PREF_PB_ENUM_AS_IDENTIFIER.getName()));
		setDefaultValue(PREF_SOURCE_COMPATIBILITY, complianceOptions.get(PREF_SOURCE_COMPATIBILITY.getName()));
		setDefaultValue(PREF_CODEGEN_TARGET_PLATFORM, complianceOptions.get(PREF_CODEGEN_TARGET_PLATFORM.getName()));
	}
}
 
Example 15
Source File: TestUtils.java    From CogniCrypt with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * This method creates a empty JavaProject in the current workspace
 * 
 * @param projectName for the JavaProject
 * @return new created JavaProject
 * @throws CoreException
 */
public static IJavaProject createJavaProject(final String projectName) throws CoreException {

	final IWorkspaceRoot workSpaceRoot = ResourcesPlugin.getWorkspace().getRoot();
	deleteProject(workSpaceRoot.getProject(projectName));

	final IProject project = workSpaceRoot.getProject(projectName);
	project.create(null);
	project.open(null);

	final IProjectDescription description = project.getDescription();
	description.setNatureIds(new String[] {JavaCore.NATURE_ID});
	project.setDescription(description, null);

	final IJavaProject javaProject = JavaCore.create(project);

	final IFolder binFolder = project.getFolder("bin");
	binFolder.create(false, true, null);
	javaProject.setOutputLocation(binFolder.getFullPath(), null);

	final List<IClasspathEntry> entries = new ArrayList<IClasspathEntry>();
	final IVMInstall vmInstall = JavaRuntime.getDefaultVMInstall();
	final LibraryLocation[] locations = JavaRuntime.getLibraryLocations(vmInstall);
	for (final LibraryLocation element : locations) {
		entries.add(JavaCore.newLibraryEntry(element.getSystemLibraryPath(), null, null));
	}
	// add libs to project class path
	javaProject.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), null);

	final IFolder sourceFolder = project.getFolder("src");
	sourceFolder.create(false, true, null);

	final IPackageFragmentRoot packageRoot = javaProject.getPackageFragmentRoot(sourceFolder);
	final IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
	final IClasspathEntry[] newEntries = new IClasspathEntry[oldEntries.length + 1];
	System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length);
	newEntries[oldEntries.length] = JavaCore.newSourceEntry(packageRoot.getPath());
	javaProject.setRawClasspath(newEntries, null);

	return javaProject;
}
 
Example 16
Source File: NewJavaProjectWizardPageOne.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public void handlePossibleJVMChange() {

			if (JavaRuntime.getDefaultVMInstall() == null) {
				fHintText.setText(NewWizardMessages.NewJavaProjectWizardPageOne_NoJREFound_link);
				fHintText.setVisible(true);
				fIcon.setImage(Dialog.getImage(Dialog.DLG_IMG_MESSAGE_WARNING));
				fIcon.setVisible(true);
				return;
			}

			String selectedCompliance= fJREGroup.getSelectedCompilerCompliance();
			if (selectedCompliance != null) {
				String defaultCompliance= JavaCore.getOption(JavaCore.COMPILER_COMPLIANCE);
				if (selectedCompliance.equals(defaultCompliance)) {
					fHintText.setVisible(false);
					fIcon.setVisible(false);
				} else {
					fHintText.setText(Messages.format(NewWizardMessages.NewJavaProjectWizardPageOne_DetectGroup_differendWorkspaceCC_message, new String[] {  BasicElementLabels.getVersionName(defaultCompliance), BasicElementLabels.getVersionName(selectedCompliance)}));
					fHintText.setVisible(true);
					fIcon.setImage(Dialog.getImage(Dialog.DLG_IMG_MESSAGE_INFO));
					fIcon.setVisible(true);
				}
				return;
			}

			selectedCompliance= JavaCore.getOption(JavaCore.COMPILER_COMPLIANCE);
			IVMInstall selectedJVM= fJREGroup.getSelectedJVM();
			if (selectedJVM == null) {
				selectedJVM= JavaRuntime.getDefaultVMInstall();
			}
			String jvmCompliance= JavaCore.VERSION_1_4;
			if (selectedJVM instanceof IVMInstall2) {
				jvmCompliance= JavaModelUtil.getCompilerCompliance((IVMInstall2) selectedJVM, JavaCore.VERSION_1_4);
			}
			if (!selectedCompliance.equals(jvmCompliance) && (JavaModelUtil.is50OrHigher(selectedCompliance) || JavaModelUtil.is50OrHigher(jvmCompliance))) {
				fHintText.setText(Messages.format(NewWizardMessages.NewJavaProjectWizardPageOne_DetectGroup_jre_message, new String[] {BasicElementLabels.getVersionName(selectedCompliance), BasicElementLabels.getVersionName(jvmCompliance)}));
				fHintText.setVisible(true);
				fIcon.setImage(Dialog.getImage(Dialog.DLG_IMG_MESSAGE_WARNING));
				fIcon.setVisible(true);
			} else {
				fHintText.setVisible(false);
				fIcon.setVisible(false);
			}

		}
 
Example 17
Source File: NewJavaProjectWizardPageOne.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private void initializeDefaultVM() {
	JavaRuntime.getDefaultVMInstall();
}
 
Example 18
Source File: MainProjectWizardPage.java    From sarl with Apache License 2.0 4 votes vote down vote up
private static void initializeDefaultVM() {
	JavaRuntime.getDefaultVMInstall();
}
 
Example 19
Source File: MainProjectWizardPage.java    From sarl with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("synthetic-access")
public void handlePossibleJVMChange() {

	if (JavaRuntime.getDefaultVMInstall() == null) {
		this.fHintText.setText(NewWizardMessages.NewJavaProjectWizardPageOne_NoJREFound_link);
		this.fHintText.setVisible(true);
		this.icon.setImage(Dialog.getImage(Dialog.DLG_IMG_MESSAGE_WARNING));
		this.icon.setVisible(true);
		return;
	}

	String selectedCompliance = MainProjectWizardPage.this.jreGroup.getSelectedCompilerCompliance();
	if (selectedCompliance != null) {
		final String defaultCompliance = JavaCore.getOption(JavaCore.COMPILER_COMPLIANCE);
		if (selectedCompliance.equals(defaultCompliance)) {
			this.fHintText.setVisible(false);
			this.icon.setVisible(false);
		} else {
			this.fHintText.setText(MessageFormat.format(
					NewWizardMessages.NewJavaProjectWizardPageOne_DetectGroup_differendWorkspaceCC_message,
					TextProcessor.process(defaultCompliance),
					TextProcessor.process(selectedCompliance)));
			this.fHintText.setVisible(true);
			this.icon.setImage(Dialog.getImage(Dialog.DLG_IMG_MESSAGE_INFO));
			this.icon.setVisible(true);
		}
		return;
	}

	selectedCompliance = JavaCore.getOption(JavaCore.COMPILER_COMPLIANCE);
	IVMInstall selectedJVM = MainProjectWizardPage.this.jreGroup.getSelectedJVM();
	if (selectedJVM == null) {
		selectedJVM = JavaRuntime.getDefaultVMInstall();
	}
	String jvmCompliance = SARLVersion.MINIMAL_JDK_VERSION_IN_SARL_PROJECT_CLASSPATH;
	if (selectedJVM instanceof IVMInstall2) {
		jvmCompliance = JavaModelUtil.getCompilerCompliance((IVMInstall2) selectedJVM,
				SARLVersion.MINIMAL_JDK_VERSION_IN_SARL_PROJECT_CLASSPATH);
	}
	if (!selectedCompliance.equals(jvmCompliance)
			&& (JavaModelUtil.is50OrHigher(selectedCompliance)
			|| JavaModelUtil.is50OrHigher(jvmCompliance))) {
		this.fHintText.setText(MessageFormat.format(
				NewWizardMessages.NewJavaProjectWizardPageOne_DetectGroup_jre_message,
				TextProcessor.process(selectedCompliance),
				TextProcessor.process(jvmCompliance)));
		this.fHintText.setVisible(true);
		this.icon.setImage(Dialog.getImage(Dialog.DLG_IMG_MESSAGE_WARNING));
		this.icon.setVisible(true);
	} else {
		this.fHintText.setVisible(false);
		this.icon.setVisible(false);
	}

}
 
Example 20
Source File: InternalImpl.java    From saros with GNU General Public License v2.0 3 votes vote down vote up
@Override
public void createJavaProject(String projectName) throws RemoteException {

  log.trace("creating java project: " + projectName);

  IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);

  try {

    project.create(null);
    project.open(null);

    IProjectDescription description = project.getDescription();
    description.setNatureIds(new String[] {JavaCore.NATURE_ID});
    project.setDescription(description, null);

    IJavaProject javaProject = JavaCore.create(project);

    Set<IClasspathEntry> entries = new HashSet<IClasspathEntry>();

    entries.add(JavaCore.newSourceEntry(javaProject.getPath().append("src"), new IPath[0]));

    IVMInstall vmInstall = JavaRuntime.getDefaultVMInstall();

    LibraryLocation[] locations = JavaRuntime.getLibraryLocations(vmInstall);

    for (LibraryLocation element : locations) {
      entries.add(JavaCore.newLibraryEntry(element.getSystemLibraryPath(), null, null));
    }

    javaProject.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), null);

  } catch (CoreException e) {
    log.error("unable to create java project '" + projectName + "' :" + e.getMessage(), e);
    throw new RemoteException(e.getMessage(), e.getCause());
  }
}