今天拿到一个需求要求实现通过端口号去杀掉进程,本来想通过进程名去杀进程如(java.exe),但是我们同一个服务器上部署了N个tomcat,意思就是有N个java进程,所以只能通过端口号去识别。
在直接在dos下执行 netstat -ano|findstr 8080,能够找到该进程的pid,然后 通过 taskkill /pid xxx 去杀掉该进程,在dos下运行没问题,可是当通过java 代码去实现的时候遇到了问题。
private static String getPID(int port) throws IOException {
String pid = "";
// dos下根据端口查看pid命令;
String netcmd = "netstat -ano|findstr " + port;
Runtime runtime = Runtime.getRuntime();
System.out.println(netcmd);
Process process2 = runtime.exec(netcmd);
BufferedReader br2 = new BufferedReader(new InputStreamReader(process2.getInputStream()));
String line = null;
while ((line = br2.readLine()) != null) {
System.out.println(line);
if ( line.indexOf("LISTEN") != -1 ){
line = line.replaceAll(" +", " ");
String[] lineArray = line.split(" ");
System.out.println(lineArray[5].trim());
pid = lineArray[5].trim();
}
}
return pid;
}
当程序运行到 line = br2.readLine() 这行的时候就阻塞了,无法得到运行的返回结果;
可是把打印出来的命令复制到dos窗口下去执行,就没问题。试试了很多次都不行,当我把命令改成
String netcmd = "netstat -ano "; 执行就没问题,百思不得其解。难道不支持,应该不会。
经过各种折腾,最后把 命令改成 String netcmd = "cmd /c netstat -ano|findstr " + port; 执行成功。
以下是全部代码:
public class StopProgram {
/**
* dos下根据端口查看pid命令 缺少参数pid
*/
private static final String NETSTART_COMMAND = "cmd /c netstat -ano|findstr ";
/**
* dos根据pid 杀进程 缺少参数pid
*/
private static final String TASKKILL_COMMAND = "taskkill /pid ";
/**
* 根据端口号获取pid
* @author 丑丑
* @creaetime 2013年12月5日 下午3:00:34
* @param port 端口号
* @return pid
* @throws IOException
*/
private String getPID(int port) {
String pid = "";
String cmd = NETSTART_COMMAND + port;
String line = exeCommand(cmd);
// System.out.println(line);
if ( line.indexOf("LISTEN") != -1 ) {
// 多个空格换成一个
line = line.replaceAll(" +", " ");
String[] lineArray = line.split(" ");
pid = lineArray[5].trim();
}
return pid;
}
/**
* 根据pid 杀进程
* @author 丑丑
* @creaetime 2013年12月5日 下午3:03:15
* @param pid 进程号
* @return true 成功,false 失败
* @throws IOException
*/
private boolean killProcess(String pid) throws IOException {
boolean flag = false;
String cmd = TASKKILL_COMMAND + pid;
String line = exeCommand(cmd);
// System.out.println(line);
if ( line.contains("成功") ) {
flag = true;
}
return flag;
}
/**
* 执行命令,得到输出内容
* @author 丑丑
* @creaetime 2013年12月5日 下午3:25:10
* @param cmd
* @return
*/
private String exeCommand(String cmd) {
StringBuffer sb = new StringBuffer();
BufferedReader br = null;
try {
Process process = Runtime.getRuntime().exec(cmd);
br = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line);
}
}
catch (IOException e) {
e.printStackTrace();
}
finally {
if ( null != br ) {
try {
br.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
return sb.toString();
}
/**
* 停止服务
* @author 丑丑
* @creaetime 2013年12月5日 下午4:55:24
* @param port
* @return
* @throws IOException
*/
public boolean stopProcess(int port) throws IOException{
String pid = getPID(port);
if(null !=pid){
return killProcess(pid);
}
return false;
}
public static void main(String[] args) {
String pid = new StopProgram().getPID(8888);
try {
new StopProgram().killProcess(pid);
}
catch (IOException e) {
e.printStackTrace();
}
}
}
搞定。