jps -l适用于Windows和Unix。您可以使用Runtime.getRuntime().exec从java程序中调用此命令。 jps -l的样本输出如下
9412 foo.bar.ClassName
9300 sun.tools.jps.Jps您可能需要解析此问题,然后检查完全限定名称,然后从相应的行获取pid。
private static void executeJps() throws IOException {
Process p = Runtime.getRuntime().exec("jps -l");
String line = null;
BufferedReader in = new BufferedReader(new InputStreamReader(
p.getInputStream(), "UTF-8"));
while ((line = in.readLine()) != null) {
String [] javaProcess = line.split(" ");
if (javaProcess.length > 1 && javaProcess[1].endsWith("ClassName")) {
System.out.println("pid => " + javaProcess[0]);
System.out.println("Fully Qualified Class Name => " +
javaProcess[1]);
}
}
}