public static IVMInstall getVMInstall(ILaunchConfiguration configuration) throws CoreException { String jre = configuration.getAttribute( IJavaLaunchConfigurationConstants.ATTR_JRE_CONTAINER_PATH, (String) null); IVMInstall vm = null; if (jre == null) { String name = configuration.getAttribute(IPDELauncherConstants.VMINSTALL, (String) null); if (name == null) { name = getDefaultVMInstallName(configuration); } vm = getVMInstall(name); if (vm == null) { throw new CoreException( LauncherUtils.createErrorStatus( NLS.bind(MDEMessages.WorkbenchLauncherConfigurationDelegate_noJRE, name))); } } else { IPath jrePath = Path.fromPortableString(jre); vm = JavaRuntime.getVMInstall(jrePath); if (vm == null) { String id = JavaRuntime.getExecutionEnvironmentId(jrePath); if (id == null) { String name = JavaRuntime.getVMInstallName(jrePath); throw new CoreException( LauncherUtils.createErrorStatus( NLS.bind(MDEMessages.WorkbenchLauncherConfigurationDelegate_noJRE, name))); } throw new CoreException( LauncherUtils.createErrorStatus(NLS.bind(MDEMessages.VMHelper_cannotFindExecEnv, id))); } } return vm; }
private void addToolsJar(ILaunchConfiguration configuration, List rtes, String path) { IRuntimeClasspathEntry tools = getToolsJar(configuration); if (tools == null) { if (path != null) { // use the global entry rtes.add(JavaRuntime.newArchiveRuntimeClasspathEntry(new Path(path))); } else { // use the default vm install to try to find a tools.jar IVMInstall install = JavaRuntime.getDefaultVMInstall(); if (install != null) { IAntClasspathEntry entry = AntCorePlugin.getPlugin() .getPreferences() .getToolsJarEntry(new Path(install.getInstallLocation().getAbsolutePath())); if (entry != null) { rtes.add( JavaRuntime.newArchiveRuntimeClasspathEntry( new Path(entry.getEntryURL().getPath()))); } } } } else { rtes.add(tools); } }
@Override protected void addUserEntries(ClasspathModel model) throws CoreException { if (supportLibs != null && supportLibs.length > 0) { for (URL supportLib : supportLibs) { model.addEntry( ClasspathModel.USER, JavaRuntime.newStringVariableClasspathEntry( new Path(supportLib.getPath()).toOSString())); } } for (IPath libRoot : libRoots) { File[] libFiles = libRoot .toFile() .listFiles( new FilenameFilter() { public boolean accept(File dir, String name) { return name.endsWith(".jar"); } }); if (!CoreUtil.isNullOrEmpty(libFiles)) { for (File libFile : libFiles) { model.addEntry( ClasspathModel.USER, JavaRuntime.newStringVariableClasspathEntry(libFile.getAbsolutePath())); } } } for (String portalLib : portalLibs) { model.addEntry( ClasspathModel.USER, JavaRuntime.newStringVariableClasspathEntry( portalDir.append("WEB-INF/lib").append(portalLib).toOSString())); } if (userLibs != null) { for (String userLib : userLibs) { model.addEntry( ClasspathModel.USER, JavaRuntime.newStringVariableClasspathEntry( portalDir.append("WEB-INF/lib").append(userLib).toOSString())); } } else { for (String jarFile : this.portalDir.append("WEB-INF/lib").toFile().list()) { if (jarFile.endsWith(".jar")) { model.addEntry( ClasspathModel.USER, JavaRuntime.newStringVariableClasspathEntry( portalDir.append("WEB-INF/lib").append(jarFile).toOSString())); } } } }
public void testArchiveLocationMemento() throws Exception { IVMInstall vm = JavaRuntime.getDefaultVMInstall(); IJavaSourceLocation location = new ArchiveSourceLocation( JavaRuntime.getLibraryLocations(vm)[0].getSystemLibraryPath().toOSString(), null); String memento = location.getMemento(); IJavaSourceLocation restored = new ArchiveSourceLocation(); restored.initializeFrom(memento); assertEquals("archive locations should be equal", location, restored); }
/** * @return the resolved classpath for the launch configuration. All entries, not just user * entries, are returned. * @throws CoreException */ public static String[] resolveClasspath(ILaunchConfiguration config) throws CoreException { IRuntimeClasspathEntry[] entries = JavaRuntime.resolveRuntimeClasspath( JavaRuntime.computeUnresolvedRuntimeClasspath(config), config); String[] paths = new String[entries.length]; for (int i = 0; i < entries.length; i++) { paths[i] = entries[i].getLocation(); } return paths; }
private void addSWTJars(List rtes) { if (fgSWTEntries == null) { fgSWTEntries = new ArrayList(); Bundle bundle = Platform.getBundle("org.eclipse.swt"); // $NON-NLS-1$ BundleDescription description = Platform.getPlatformAdmin().getState(false).getBundle(bundle.getBundleId()); BundleDescription[] fragments = description.getFragments(); for (int i = 0; i < fragments.length; i++) { Bundle fragmentBundle = Platform.getBundle(fragments[i].getName()); URL bundleURL; try { bundleURL = FileLocator.resolve(fragmentBundle.getEntry("/")); // $NON-NLS-1$ } catch (IOException e) { AntLaunching.log(e); continue; } String urlFileName = bundleURL.getFile(); if (urlFileName.startsWith("file:")) { // $NON-NLS-1$ try { urlFileName = new URL(urlFileName).getFile(); if (urlFileName.endsWith("!/")) { // $NON-NLS-1$ urlFileName = urlFileName.substring(0, urlFileName.length() - 2); } } catch (MalformedURLException e) { AntLaunching.log(e); continue; } } IPath fragmentPath = new Path(urlFileName); if (fragmentPath.getFileExtension() != null) { // JAR file fgSWTEntries.add(JavaRuntime.newArchiveRuntimeClasspathEntry(fragmentPath)); } else { // folder File bundleFolder = fragmentPath.toFile(); if (!bundleFolder.isDirectory()) { continue; } String[] names = bundleFolder.list( new FilenameFilter() { public boolean accept(File dir, String name) { return name.endsWith(".jar"); // $NON-NLS-1$ } }); for (int j = 0; j < names.length; j++) { String jarName = names[j]; fgSWTEntries.add( JavaRuntime.newArchiveRuntimeClasspathEntry(fragmentPath.append(jarName))); } } } } rtes.addAll(fgSWTEntries); }
/** * Get the default VMInstall name using the available info in the config, using the JavaProject if * available. * * @param configuration Launch configuration to check * @return name of the VMInstall * @throws CoreException thrown if there's a problem getting the VM name */ public static String getDefaultVMInstallName(ILaunchConfiguration configuration) throws CoreException { IJavaProject javaProject = JavaRuntime.getJavaProject(configuration); IVMInstall vmInstall = null; if (javaProject != null) { vmInstall = JavaRuntime.getVMInstall(javaProject); } if (vmInstall != null) { return vmInstall.getName(); } return VMUtil.getDefaultVMInstallName(); }
@SuppressWarnings({"rawtypes", "unchecked", "resource"}) @Override public Object createExecutableExtension(String propertyName) throws CoreException { try { String className = attributes.get(propertyName); IProject project = getProject(); IJavaProject javaProject = JavaCore.create(project); String[] classPathEntries = JavaRuntime.computeDefaultRuntimeClassPath(javaProject); List<URL> urlList = new ArrayList<URL>(); for (int i = 0; i < classPathEntries.length; i++) { String entry = classPathEntries[i]; IPath path = new Path(entry); URL url = path.toFile().toURI().toURL(); urlList.add(url); } ClassLoader parentClassLoader = javaProject.getClass().getClassLoader(); URL[] urls = (URL[]) urlList.toArray(new URL[urlList.size()]); URLClassLoader classLoader = new URLClassLoader(urls, parentClassLoader); ClassLoader cl = classLoader.getParent(); Class clazz = classLoader.loadClass(className); return clazz.getConstructor().newInstance(); } catch (Exception ex) { ex.printStackTrace(); throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, ex.getMessage())); } }
private void addSystemLibraries() throws JavaModelException { IClasspathEntry[] oldEntries = javaProject.getRawClasspath(); IClasspathEntry[] newEntries = new IClasspathEntry[oldEntries.length + 1]; System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length); newEntries[oldEntries.length] = JavaRuntime.getDefaultJREContainerEntry(); javaProject.setRawClasspath(newEntries, null); }
void doDebug(IProgressMonitor monitor) throws InterruptedException { monitor.setTaskName( "Connecting debugger " + session.getName() + " to " + session.getHost() + ":" + session.getJdb()); Map<String, String> parameters = new HashMap<String, String>(); parameters.put("hostname", session.getHost()); parameters.put("port", session.getJdb() + ""); parameters.put("timeout", session.getTimeout() + ""); IVMConnector connector = JavaRuntime.getDefaultVMConnector(); while (!monitor.isCanceled()) { try { connector.connect(parameters, monitor, launch); break; } catch (Exception e) { Thread.sleep(500); } } }
/** * Create a new IVMInstall of DefaultVM install type. * * @return */ public static IVMInstall createVM(File location) { @SuppressWarnings("restriction") IVMInstallType vmType = JavaRuntime.getVMInstallType(ID_STANDARD_VM_TYPE); IVMInstall vm = vmType.createVMInstall(createVMId(vmType)); vm.setInstallLocation(location); return vm; }
boolean isUnmodifiable(Object element) { if (element instanceof IVMInstall) { IVMInstall vm = (IVMInstall) element; return JavaRuntime.isContributedVMInstall(vm.getId()); } return false; }
/** Performs the edit VM action when the Edit... button is pressed */ private void editVM() { IStructuredSelection selection = (IStructuredSelection) fVMList.getSelection(); VMStandin vm = (VMStandin) selection.getFirstElement(); if (vm == null) { return; } if (JavaRuntime.isContributedVMInstall(vm.getId())) { VMDetailsDialog dialog = new VMDetailsDialog(getShell(), vm); dialog.open(); } else { EditVMInstallWizard wizard = new EditVMInstallWizard(vm, fVMs.toArray(new IVMInstall[fVMs.size()])); WizardDialog dialog = new WizardDialog(getShell(), wizard); if (dialog.open() == Window.OK) { VMStandin result = wizard.getResult(); if (result != null) { // replace with the edited VM int index = fVMs.indexOf(vm); fVMs.remove(index); fVMs.add(index, result); fVMList.setSelection(new StructuredSelection(result)); fVMList.refresh(true); } } } }
ISourceContainer[] getSourceContainers(String location, String id) throws CoreException { ISourceContainer[] containers = (ISourceContainer[]) fSourceContainerMap.get(location); if (containers != null) { return containers; } ArrayList result = new ArrayList(); ModelEntry entry = MonitorRegistry.findEntry(id); boolean match = false; IMonitorModelBase[] models = entry.getWorkspaceModels(); for (int i = 0; i < models.length; i++) { if (isPerfectMatch(models[i], new Path(location))) { IResource resource = models[i].getUnderlyingResource(); // if the plug-in matches a workspace model, // add the project and any libraries not coming via a container // to the list of source containers, in that order if (resource != null) { addProjectSourceContainers(resource.getProject(), result); } match = true; break; } } if (!match) { File file = new File(location); if (file.isFile()) { // in case of linked plug-in projects that map to an external JARd plug-in, // use source container that maps to the library in the linked project. ISourceContainer container = getArchiveSourceContainer(location); if (container != null) { containers = new ISourceContainer[] {container}; fSourceContainerMap.put(location, containers); return containers; } } models = entry.getExternalModels(); for (int i = 0; i < models.length; i++) { if (isPerfectMatch(models[i], new Path(location))) { // try all source zips found in the source code locations IClasspathEntry[] entries = MDEClasspathContainer.getExternalEntries(models[i]); for (int j = 0; j < entries.length; j++) { IRuntimeClasspathEntry rte = convertClasspathEntry(entries[j]); if (rte != null) result.add(rte); } break; } } } IRuntimeClasspathEntry[] entries = (IRuntimeClasspathEntry[]) result.toArray(new IRuntimeClasspathEntry[result.size()]); containers = JavaRuntime.getSourceContainers(entries); fSourceContainerMap.put(location, containers); return containers; }
/** * Sets the workspace default VM on the given working copy * * @param config * @since 3.5 */ @SuppressWarnings("deprecation") public static void setVM(ILaunchConfigurationWorkingCopy config) { IVMInstall vm = JavaRuntime.getDefaultVMInstall(); String vmName = vm.getName(); String vmTypeID = vm.getVMInstallType().getId(); config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_INSTALL_NAME, vmName); config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_INSTALL_TYPE, vmTypeID); }
private String getDefaultJVMName() { IVMInstall install = JavaRuntime.getDefaultVMInstall(); if (install != null) { return install.getName(); } else { return Messages.NewBlackBerryProjectWizardPageOne_UnknownDefaultJRE_name; } }
public static IVMInstall ensureJavaXXdefaultVM(String version) throws CoreException { // Before doing anything check the current default VM IVMInstall vm = JavaRuntime.getDefaultVMInstall(); if (JavaUtils.isJavaXX(vm, version)) { return vm; // Done! } vm = getJavaXXVM(version); if (vm == null) { vm = JavaUtils.createVM(getVMLocation(version)); } if (!JavaUtils.isJavaXX(vm, version)) { throw new Error("vm at " + vm.getInstallLocation() + " doesn't look like a Java " + version); } JavaRuntime.setDefaultVMInstall(vm, new NullProgressMonitor()); return vm; }
public static IVMInstall getVMInstall(String name) { if (name != null) { IVMInstall[] installs = VMUtil.getAllVMInstances(); for (int i = 0; i < installs.length; i++) { if (installs[i].getName().equals(name)) return installs[i]; } } return JavaRuntime.getDefaultVMInstall(); }
/** * Returns the JRE container path * * @return The JRE container path */ public IPath getJREContainerPath() { if (_useProjectRE.isSelected()) { int index = _RECombo.getSelectionIndex(); if (index >= 0 && index < _installedVMs.length) { // paranoia return JavaRuntime.newJREContainerPath(_installedVMs[index]); } } return null; }
public static boolean isExistingVMName(String name) { for (IVMInstall vm : JavaRuntime.getVMInstallType(StandardVMType.ID_STANDARD_VM_TYPE).getVMInstalls()) { if (vm.getName().equals(name)) { return true; } } return false; }
/** * Returns the tools.jar to use for this launch configuration, or <code>null</code> if none. * * @param configuration configuration to resolve a tools.jar for * @return associated tools.jar archive, or <code>null</code> */ private IRuntimeClasspathEntry getToolsJar(ILaunchConfiguration configuration) { try { IVMInstall install = JavaRuntime.computeVMInstall(configuration); if (install != null) { IAntClasspathEntry entry = AntCorePlugin.getPlugin() .getPreferences() .getToolsJarEntry(new Path(install.getInstallLocation().getAbsolutePath())); if (entry != null) { return JavaRuntime.newArchiveRuntimeClasspathEntry( new Path(entry.getEntryURL().getPath())); } } } catch (CoreException ce) { // likely dealing with a non-Java project } return null; }
public static List<IVMInstall> getAllVMs() { List<IVMInstall> result = new ArrayList<IVMInstall>(); IVMInstallType[] types = JavaRuntime.getVMInstallTypes(); for (IVMInstallType type : types) { for (IVMInstall vm : type.getVMInstalls()) { result.add(vm); } } return result; }
private static IPath[] getClasspath(ILaunchConfiguration configuration) throws CoreException { IRuntimeClasspathEntry[] entries = JavaRuntime.computeUnresolvedRuntimeClasspath(configuration); entries = JavaRuntime.resolveRuntimeClasspath(entries, configuration); ArrayList<IPath> userEntries = new ArrayList<IPath>(entries.length); for (int i = 0; i < entries.length; i++) { if (entries[i].getClasspathProperty() == IRuntimeClasspathEntry.USER_CLASSES) { String location = entries[i].getLocation(); if (location != null) { IPath entry = Path.fromOSString(location); if (!userEntries.contains(entry)) { userEntries.add(entry); } } } } return userEntries.toArray(new IPath[userEntries.size()]); }
/** * Get selected JVM. * * @return Selected JVM or <code>null</code> if one is not found */ public IVMInstall getSelectedJVM() { if (_useProjectRE.isSelected()) { int index = _RECombo.getSelectionIndex(); if (index >= 0 && index < _installedVMs.length) { // paranoia return _installedVMs[index]; } return null; } // user selects workspace default JRE return JavaRuntime.getDefaultVMInstall(); }
public void testJavaSourceLocatorMemento() throws Exception { IJavaSourceLocation location1 = new JavaProjectSourceLocation(get14Project()); File dir = ResourcesPlugin.getWorkspace().getRoot().getLocation().toFile(); IJavaSourceLocation location2 = new DirectorySourceLocation(dir); IVMInstall vm = JavaRuntime.getDefaultVMInstall(); IJavaSourceLocation location3 = new ArchiveSourceLocation( JavaRuntime.getLibraryLocations(vm)[0].getSystemLibraryPath().toOSString(), null); JavaSourceLocator locator = new JavaSourceLocator(new IJavaSourceLocation[] {location1, location2, location3}); String memento = locator.getMemento(); JavaSourceLocator restored = new JavaSourceLocator(); restored.initializeFromMemento(memento); IJavaSourceLocation[] locations = restored.getSourceLocations(); assertEquals("wrong number of source locations", 3, locations.length); assertEquals("1st locations not equal", location1, locations[0]); assertEquals("2nd locations not equal", location2, locations[1]); assertEquals("3rd locations not equal", location3, locations[2]); }
private IRuntimeClasspathEntry convertClasspathEntry(IClasspathEntry entry) { if (entry == null) return null; IPath srcPath = entry.getSourceAttachmentPath(); if (srcPath != null && srcPath.segmentCount() > 0) { IRuntimeClasspathEntry rte = JavaRuntime.newArchiveRuntimeClasspathEntry(entry.getPath()); rte.setSourceAttachmentPath(srcPath); rte.setSourceAttachmentRootPath(entry.getSourceAttachmentRootPath()); return rte; } return null; }
/* (non-Javadoc) * @see org.eclipse.jdt.launching.IRuntimeClasspathEntry2#getRuntimeClasspathEntries(org.eclipse.debug.core.ILaunchConfiguration) */ public IRuntimeClasspathEntry[] getRuntimeClasspathEntries(ILaunchConfiguration configuration) throws CoreException { boolean separateVM = AntLaunchingUtil.isSeparateJREAntBuild(configuration); boolean setInputHandler = configuration.getAttribute(AntLaunching.SET_INPUTHANDLER, true); AntCorePreferences prefs = AntCorePlugin.getPlugin().getPreferences(); IAntClasspathEntry[] antClasspathEntries = prefs.getContributedClasspathEntries(); IAntClasspathEntry[] userEntries = prefs.getAdditionalClasspathEntries(); List rtes = new ArrayList(antClasspathEntries.length + userEntries.length); IAntClasspathEntry entry; for (int i = 0; i < antClasspathEntries.length; i++) { entry = antClasspathEntries[i]; if (!separateVM || (separateVM && !entry.isEclipseRuntimeRequired())) { rtes.add(JavaRuntime.newStringVariableClasspathEntry(entry.getLabel())); } } boolean haveToolsEntry = false; String path; for (int i = 0; i < userEntries.length; i++) { entry = userEntries[i]; path = entry.getLabel(); IPath toolsPath = new Path(path); if (toolsPath.lastSegment().equals("tools.jar")) { // $NON-NLS-1$ haveToolsEntry = true; // replace with dynamically resolved tools.jar based on // the JRE being used addToolsJar(configuration, rtes, path); } else { rtes.add(JavaRuntime.newStringVariableClasspathEntry(path)); } } if (!haveToolsEntry) { addToolsJar(configuration, rtes, null); } if (setInputHandler && separateVM) { addSWTJars(rtes); } return (IRuntimeClasspathEntry[]) rtes.toArray(new IRuntimeClasspathEntry[rtes.size()]); }
private static List<String> appendLaunchClasspath( List<String> l, String projectName, ILaunchConfiguration config, IClasspathEntry[] adding, IJavaProject jp, int high) { initJefHome(); Set<IClasspathEntry> set = new HashSet<IClasspathEntry>(); try { if (l == null) { l = new ArrayList<String>(); l.add(JavaRuntime.computeJREEntry(jp).getMemento()); l.add( String.format( "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\r\n<runtimeClasspathEntry id=\"org.eclipse.jdt.launching.classpathentry.defaultClasspath\">\r\n<memento exportedEntriesOnly=\"false\" project=\"%s\"/></runtimeClasspathEntry>\r\n", projectName)); } else { for (String s : l) { Document doc = XMLUtils.loadDocumentByString(s); if (doc.getDocumentElement().hasAttribute("type")) { IRuntimeClasspathEntry rcp = new org.eclipse.jdt.internal.launching.RuntimeClasspathEntry( doc.getDocumentElement()); set.add(rcp.getClasspathEntry()); } } } int i = 0; for (IClasspathEntry cp : adding) { if (!set.contains(cp)) { IRuntimeClasspathEntry rcp = new org.eclipse.jdt.internal.launching.RuntimeClasspathEntry(cp); if (i < high) { rcp.setClasspathProperty(2); } else { rcp.setClasspathProperty(3); } l.add(rcp.getMemento()); set.add(cp); i++; } } } catch (CoreException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } return l; }
/** * Verifies that the classpath entry is valid and won't cause the launch to fail. * * @param entry The entry to validate. * @param serverProject The project that the test server will run out of. * @return true if it's valid */ public static boolean classpathEntryIsValid(String entry, IProject serverProject) { try { ILaunchConfigurationType type = DebugPlugin.getDefault() .getLaunchManager() .getLaunchConfigurationType(IJavaLaunchConfigurationConstants.ID_JAVA_APPLICATION); ILaunchConfigurationWorkingCopy copy = type.newInstance(null, "ezeTemp"); // $NON-NLS-1$ copy.setAttribute( IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, serverProject.getName()); copy.setAttribute(IJavaLaunchConfigurationConstants.ATTR_DEFAULT_CLASSPATH, false); List<String> classpath = new ArrayList<String>(1); classpath.add(entry); copy.setAttribute(IJavaLaunchConfigurationConstants.ATTR_CLASSPATH, classpath); JavaRuntime.resolveRuntimeClasspath( JavaRuntime.computeUnresolvedRuntimeClasspath(copy), copy); return true; } catch (CoreException ce) { return false; } }
private void addJreClasspath() { try { IRuntimeClasspathEntry computeJREEntry = JavaRuntime.computeJREEntry(javaProject); if (computeJREEntry == null) { return; } IRuntimeClasspathEntry[] jreEntries = JavaRuntime.resolveRuntimeClasspathEntry(computeJREEntry, javaProject); if (jreEntries.length != 0) { for (IRuntimeClasspathEntry jreEntry : jreEntries) { addToClasspath(jreEntry.getClasspathEntry().getPath().toFile()); } return; } } catch (JavaModelException e) { KotlinLogger.logAndThrow(e); } catch (CoreException e) { KotlinLogger.logAndThrow(e); } }