Prefer to use jps/java under jdk, not jre. close #338

This commit is contained in:
hengyunabc
2018-12-06 20:39:46 +08:00
parent 9f8b5fc3de
commit f47596b2c2
@@ -5,6 +5,8 @@ import java.io.IOException;
import java.io.InputStream;
import java.lang.management.ManagementFactory;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
@@ -94,6 +96,8 @@ public class ProcessUtils {
jps = jpsFile.getAbsolutePath();
}
AnsiLog.debug("Try use jps to lis java process, jps: " + jps);
String[] command = null;
if (v) {
command = new String[] { jps, "-v" };
@@ -212,30 +216,83 @@ public class ProcessUtils {
String javaHome = System.getProperty("java.home");
String[] paths = { "bin/java", "bin/java.exe", "../bin/java", "../bin/java.exe" };
List<File> javaList = new ArrayList<File>();
for (String path : paths) {
File jpsFile = new File(javaHome, path);
if (jpsFile.exists()) {
return jpsFile;
File javaFile = new File(javaHome, path);
if (javaFile.exists()) {
AnsiLog.debug("Found java: " + javaFile.getAbsolutePath());
javaList.add(javaFile);
}
}
AnsiLog.debug("can not find java under current java home: " + javaHome);
return null;
if (javaList.isEmpty()) {
AnsiLog.debug("Can not find java under current java home: " + javaHome);
return null;
}
// find the shortest path, jre path longer than jdk path
if (javaList.size() > 1) {
Collections.sort(javaList, new Comparator<File>() {
@Override
public int compare(File file1, File file2) {
try {
return file1.getCanonicalPath().length() - file2.getCanonicalPath().length();
} catch (IOException e) {
// ignore
}
return -1;
}
});
}
return javaList.get(0);
}
private static File findJps() {
String javaHome = System.getProperty("java.home");
String[] paths = { "bin/jps", "bin/jps.exe", "../bin/jps", "../bin/jps.exe" };
List<File> jpsList = new ArrayList<File>();
for (String path : paths) {
File jpsFile = new File(javaHome, path);
if (jpsFile.exists()) {
return jpsFile;
AnsiLog.debug("Found jps: " + jpsFile.getAbsolutePath());
jpsList.add(jpsFile);
}
}
AnsiLog.debug("can not find jps under current java home: " + javaHome);
return null;
if (jpsList.isEmpty()) {
AnsiLog.debug("Can not find jps under :" + javaHome);
String javaHomeEnv = System.getenv("JAVA_HOME");
AnsiLog.debug("Try to find jps under env JAVA_HOME :" + javaHomeEnv);
for (String path : paths) {
File jpsFile = new File(javaHomeEnv, path);
if (jpsFile.exists()) {
AnsiLog.debug("Found jps: " + jpsFile.getAbsolutePath());
jpsList.add(jpsFile);
}
}
}
if (jpsList.isEmpty()) {
AnsiLog.debug("Can not find jps under current java home: " + javaHome);
return null;
}
// find the shortest path, jre path longer than jdk path
if (jpsList.size() > 1) {
Collections.sort(jpsList, new Comparator<File>() {
@Override
public int compare(File file1, File file2) {
try {
return file1.getCanonicalPath().length() - file2.getCanonicalPath().length();
} catch (IOException e) {
// ignore
}
return -1;
}
});
}
return jpsList.get(0);
}
}