1 11 package org.eclipse.jdt.internal.launching; 12 13 14 import org.eclipse.core.resources.IWorkspace; 15 import org.eclipse.core.resources.IWorkspaceDescription; 16 import org.eclipse.core.resources.ResourcesPlugin; 17 import org.eclipse.core.runtime.CoreException; 18 import org.eclipse.core.runtime.IPath; 19 import org.eclipse.core.runtime.IProgressMonitor; 20 import org.eclipse.core.runtime.NullProgressMonitor; 21 import org.eclipse.jdt.core.ClasspathVariableInitializer; 22 import org.eclipse.jdt.core.JavaCore; 23 import org.eclipse.jdt.launching.IVMInstall; 24 import org.eclipse.jdt.launching.JavaRuntime; 25 import org.eclipse.jdt.launching.LibraryLocation; 26 27 public class JavaClasspathVariablesInitializer extends ClasspathVariableInitializer { 28 29 33 private IProgressMonitor fMonitor; 34 35 38 public void initialize(String variable) { 39 IVMInstall vmInstall= JavaRuntime.getDefaultVMInstall(); 40 if (vmInstall != null) { 41 IPath newPath= null; 42 LibraryLocation[] locations= JavaRuntime.getLibraryLocations(vmInstall); 43 LibraryLocation rtjar = null; 45 LibraryLocation classeszip = null; 46 for (int i = 0; i < locations.length; i++) { 47 LibraryLocation location = locations[i]; 48 String name = location.getSystemLibraryPath().lastSegment(); 49 if (name.equalsIgnoreCase("rt.jar")) { rtjar = location; 51 } else if (name.equalsIgnoreCase("classes.zip")) { classeszip = location; 53 } 54 } 55 LibraryLocation systemLib = rtjar; 57 if (systemLib == null) { 58 systemLib = classeszip; 59 } 60 if (systemLib == null && locations.length > 0) { 61 systemLib = locations[0]; 62 } 63 if (systemLib != null) { 64 if (variable.equals(JavaRuntime.JRELIB_VARIABLE)) { 65 newPath= systemLib.getSystemLibraryPath(); 66 } else if (variable.equals(JavaRuntime.JRESRC_VARIABLE)) { 67 newPath= systemLib.getSystemLibrarySourcePath(); 68 } else if (variable.equals(JavaRuntime.JRESRCROOT_VARIABLE)){ 69 newPath= systemLib.getPackageRootPath(); 70 } 71 if (newPath == null) { 72 return; 73 } 74 IWorkspace workspace= ResourcesPlugin.getWorkspace(); 75 IWorkspaceDescription wsDescription= workspace.getDescription(); 76 boolean wasAutobuild= wsDescription.isAutoBuilding(); 77 try { 78 setAutobuild(workspace, false); 79 setJREVariable(newPath, variable); 80 } catch (CoreException ce) { 81 LaunchingPlugin.log(ce); 82 return; 83 } finally { 84 try { 85 setAutobuild(workspace, wasAutobuild); 86 } catch (CoreException ce) { 87 LaunchingPlugin.log(ce); 88 } 89 } 90 } 91 } 92 } 93 94 private void setJREVariable(IPath newPath, String var) throws CoreException { 95 JavaCore.setClasspathVariable(var, newPath, getMonitor()); 96 } 97 98 private boolean setAutobuild(IWorkspace ws, boolean newState) throws CoreException { 99 IWorkspaceDescription wsDescription= ws.getDescription(); 100 boolean oldState= wsDescription.isAutoBuilding(); 101 if (oldState != newState) { 102 wsDescription.setAutoBuilding(newState); 103 ws.setDescription(wsDescription); 104 } 105 return oldState; 106 } 107 108 protected IProgressMonitor getMonitor() { 109 if (fMonitor == null) { 110 return new NullProgressMonitor(); 111 } 112 return fMonitor; 113 } 114 115 } 116 | Popular Tags |