{
String jarName = Os.isFamily(Os.FAMILY_MAC) ? "classes.jar" : "rt.jar";
// doing a straight JavaCore.setClasspathVariable() doesn't work, so we need
// to modify the library path of the default vm install.
try{
IVMInstall vm = JavaRuntime.getDefaultVMInstall();
LibraryLocation[] locations = JavaRuntime.getLibraryLocations(vm);
LibraryLocation[] newLocations = new LibraryLocation[locations.length];
for(int ii = 0; ii < locations.length; ii++){
IPath libraryPath = locations[ii].getSystemLibraryPath();
// eclipse didn't find src.zip, so search other known locations.
if (libraryPath.lastSegment().equals(jarName) &&
(locations[ii].getSystemLibrarySourcePath().isEmpty() ||
!locations[ii].getSystemLibrarySourcePath().toFile().exists()))
{
IPath jreSrc = null;
logger.debug("Attempting to locate jre src.zip for JAVA_HOME: {}",
SystemUtils.JAVA_HOME);
for (int jj = 0; jj < SRC_LOCATIONS.length; jj++){
String location = SRC_LOCATIONS[jj];
// absolute path
if (location.startsWith("/") ||
location.indexOf(':') != -1)
{
jreSrc = new Path(location);
// relative path
}else{
jreSrc = libraryPath.removeLastSegments(3).append(location);
}
logger.debug("Trying location: {}", jreSrc);
if(jreSrc.toFile().exists()){
break;
}
}
// other possibilities on windows machines:
// library path: C:/.../jre<version>/
// src archive: C:/.../jdk<version>/src.zip
// or
// library path: C:/.../jre<major>/
// src archive: C:/.../jdk1.<major>.<minor>_<patch>/src.zip
if (!jreSrc.toFile().exists() && Os.isFamily(Os.FAMILY_WINDOWS)){
String path = libraryPath.toOSString()
.replaceFirst("\\\\(lib\\\\)rt.jar", "");
// first scenerio
String altHome = path.replaceFirst(
"jre(\\d+\\.\\d+\\.\\d+_\\d+)", "jdk$1");
if (!altHome.equals(path)){
jreSrc = new Path(altHome).append("src.zip");
}
// second scenerio
if (!jreSrc.toFile().exists()){
String base = FileUtils.getBaseName(path);
final String major = base.replaceFirst("^jre(\\d)$", "$1");
if (!major.equals(base)){
File dir = new File(FileUtils.getFullPath(path));
String[] jdks = dir.list(new FilenameFilter(){
private final Pattern JDK =
Pattern.compile("jdk\\d+\\." + major + "\\.\\d+_\\d+");
public boolean accept(File dir, String name){
return JDK.matcher(name).matches();
}
});
for (String jdk : jdks){
jreSrc = new Path(dir.toString()).append(jdk).append("src.zip");
if (jreSrc.toFile().exists()){
break;
}
}
}
}
}
// jre src found.
if(jreSrc.toFile().exists()){
logger.info("Setting '{}' to '{}'",
JavaRuntime.JRESRC_VARIABLE, jreSrc);
newLocations[ii] = new LibraryLocation(
locations[ii].getSystemLibraryPath(),
jreSrc,
locations[ii].getPackageRootPath(),
locations[ii].getJavadocLocation());
// jre src not found.
}else{
logger.debug(
"Unable to locate jre src.zip for JAVA_HOME: " +
SystemUtils.JAVA_HOME);
newLocations[ii] = new LibraryLocation(
locations[ii].getSystemLibraryPath(),
Path.EMPTY,
locations[ii].getPackageRootPath(),
locations[ii].getJavadocLocation());
}
}else{
newLocations[ii] = locations[ii];
}
}
vm.setLibraryLocations(newLocations);
}catch(Exception e){
logger.error("", e);
}
}