From f47596b2c273bb6f137d94ff976d243f464163ad Mon Sep 17 00:00:00 2001 From: hengyunabc Date: Thu, 6 Dec 2018 20:39:46 +0800 Subject: [PATCH] Prefer to use jps/java under jdk, not jre. close #338 --- .../com/taobao/arthas/boot/ProcessUtils.java | 73 +++++++++++++++++-- 1 file changed, 65 insertions(+), 8 deletions(-) diff --git a/boot/src/main/java/com/taobao/arthas/boot/ProcessUtils.java b/boot/src/main/java/com/taobao/arthas/boot/ProcessUtils.java index fae61c04c..698f99ad9 100644 --- a/boot/src/main/java/com/taobao/arthas/boot/ProcessUtils.java +++ b/boot/src/main/java/com/taobao/arthas/boot/ProcessUtils.java @@ -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 javaList = new ArrayList(); 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() { + @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 jpsList = new ArrayList(); 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() { + @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); } }