Java、windows:获取给定PID的进程名称
在windows上运行 我需要从我的程序中知道哪些进程(如Skype)正在端口Java、windows:获取给定PID的进程名称,java,process,jetty,port,pid,Java,Process,Jetty,Port,Pid,在windows上运行 我需要从我的程序中知道哪些进程(如Skype)正在端口:80上运行 我应该能够通过 netstat-o-n-a | findstr 0.0:80 从Java中获取具有给定PID的进程名称的最佳方法是什么 如果有任何方法可以从Java中获取在端口:80上运行的进程的名称,我也希望这样 我之所以需要它,是因为我的应用程序启动了一个jetty web服务器,它使用端口:80。我想警告用户,另一个服务已经在运行端口:80,以防万一。因此我使用两个进程使它工作。一个用于获取PID,
:80上运行
我应该能够通过
netstat-o-n-a | findstr 0.0:80
从Java中获取具有给定PID的进程名称的最佳方法是什么
如果有任何方法可以从Java中获取在端口:80
上运行的进程的名称,我也希望这样
我之所以需要它,是因为我的应用程序启动了一个jetty web服务器,它使用端口:80
。我想警告用户,另一个服务已经在运行端口:80
,以防万一。因此我使用两个进程使它工作。一个用于获取PID,另一个用于获取名称
这是:
private String getPIDRunningOnPort80() {
String cmd = "cmd /c netstat -o -n -a | findstr 0.0:80";
Runtime rt = Runtime.getRuntime();
Process p = null;
try {
p = rt.exec(cmd);
} catch (IOException e) {
e.printStackTrace();
}
StringBuffer sbInput = new StringBuffer();
BufferedReader brInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader brError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
String line;
try {
while ((line = brError.readLine()) != null) {
}
} catch (IOException e) {
e.printStackTrace();
}
try {
while ((line = brInput.readLine()) != null) {
sbInput.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
}
try {
p.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
p.destroy();
String resultString = sbInput.toString();
resultString = resultString.substring(resultString.lastIndexOf(" ", resultString.length())).replace("\n", "");
return resultString;
}
private String getProcessNameFromPID(String pid) {
Process p = null;
try {
p = Runtime.getRuntime().exec("tasklist");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
StringBuffer sbInput = new StringBuffer();
BufferedReader brInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
String foundLine = "UNKNOWN";
try {
while ((line = brInput.readLine()) != null) {
if (line.contains(pid)){
foundLine = line;
}
System.out.println(line);
sbInput.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
}
try {
p.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
p.destroy();
String result = foundLine.substring(0, foundLine.indexOf(" "));
return result;
}
您是在Windows还是Linux上运行?