Slight;y better logic to look for tools.jar

Favor JAVA_HOME env var over system prop.

Look both in java home and its parrent for "lib/tools.jar"
This commit is contained in:
Kris De Volder
2018-01-26 12:38:41 -08:00
parent 23ea40d89b
commit bab9f98d9b
2 changed files with 61 additions and 5 deletions

View File

@@ -12,8 +12,11 @@ package org.springframework.tooling.ls.eclipse.commons;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.internal.launching.StandardVMType;
import org.eclipse.lsp4e.server.ProcessStreamConnectionProvider;
@@ -50,11 +53,58 @@ public class STS4LanguageServerProcessStreamConnector extends ProcessStreamConne
return null;
}
protected String getToolsJAR() {
File jre = new File(System.getProperty("java.home"));
return new File(jre.getParent(), "lib" + Path.SEPARATOR + "tools.jar").getAbsolutePath();
/**
* Different places to look for tools jar, relative to Java home.
*/
private static String [] TOOLS_JAR_PATHS = {
"../lib/tools.jar",
"lib/tools.jar"
};
protected File getToolsJAR() {
List<File> jhomes = new ArrayList<>(2);
resolve(System.getenv("JAVA_HOME"), jhomes::add);
resolve(System.getProperty("java.home"), jhomes::add);
for (File jhome : jhomes) {
for (String tjPath : TOOLS_JAR_PATHS) {
File toolsJar = new File(jhome, tjPath);
if (toolsJar.isFile()) {
return toolsJar;
}
}
}
return null;
}
private void resolve(String path, Consumer<File> requestor) {
if (path!=null) {
try {
File file = new File(path).getCanonicalFile();
if (file.exists()) {
requestor.accept(file);
return;
}
} catch (IOException e) {
//Ignore... this can happen because it tries to handle symbolic links
}
}
}
private File getJavaHomeFromEnv() {
try {
File jhome = new File(System.getenv("JAVA_HOME"));
if (jhome!=null) {
jhome = jhome.getCanonicalFile();
if (jhome.isDirectory()) {
return jhome;
}
}
} catch (IOException e) {
//Ignore... this can happen because it tries to handle symbolic links
}
return null;
}
protected String getWorkingDirLocation() {
return System.getProperty("user.dir");
}