package org.eclipse.jdt.internal.launching;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jdt.core.ClasspathVariableInitializer;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.launching.IVMInstall;
import org.eclipse.jdt.launching.JavaRuntime;
import org.eclipse.jdt.launching.LibraryLocation;
public class JavaClasspathVariablesInitializer extends ClasspathVariableInitializer {
private IProgressMonitor fMonitor;
@Override
public void initialize(String variable) {
IVMInstall vmInstall= JavaRuntime.getDefaultVMInstall();
if (vmInstall != null) {
IPath newPath= null;
LibraryLocation[] locations= JavaRuntime.getLibraryLocations(vmInstall);
LibraryLocation rtjar = null;
LibraryLocation classeszip = null;
for (int i = 0; i < locations.length; i++) {
LibraryLocation location = locations[i];
String name = location.getSystemLibraryPath().lastSegment();
if (name.equalsIgnoreCase("rt.jar")) {
rtjar = location;
} else if (name.equalsIgnoreCase("classes.zip")) {
classeszip = location;
}
}
LibraryLocation systemLib = rtjar;
if (systemLib == null) {
systemLib = classeszip;
}
if (systemLib == null && locations.length > 0) {
systemLib = locations[0];
}
if (systemLib != null) {
if (variable.equals(JavaRuntime.JRELIB_VARIABLE)) {
newPath= systemLib.getSystemLibraryPath();
} else if (variable.equals(JavaRuntime.JRESRC_VARIABLE)) {
newPath= systemLib.getSystemLibrarySourcePath();
} else if (variable.equals(JavaRuntime.JRESRCROOT_VARIABLE)){
newPath= systemLib.getPackageRootPath();
}
if (newPath == null) {
return;
}
try {
setJREVariable(newPath, variable);
}
catch (CoreException e) {
LaunchingPlugin.log(e);
}
}
}
}
private void setJREVariable(IPath newPath, String var) throws CoreException {
JavaCore.setClasspathVariable(var, newPath, getMonitor());
}
protected IProgressMonitor getMonitor() {
if (fMonitor == null) {
return new NullProgressMonitor();
}
return fMonitor;
}
}