检测某个进程是否存在
/**
* 检测某个进程是否存在
* @param name 进程名称
* @return
*/
public static boolean findProcess(String name) {
/**windwos系统*/
BufferedReader bufferedReader = null;
try {
Process proc = Runtime.getRuntime().exec("tasklist -fi " + '"' + "imagename eq " + name +'"');
bufferedReader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line = null;
while ((line = bufferedReader.readLine()) != null) {
// System.out.println("line:"+line);
if (line.contains(name)) {
return true;
}
}
return false;
} catch (Exception ex) {
ex.printStackTrace();
return false;
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (Exception ex) {}
}
}
return false;
}
检测某个端口是否占用
/**
* 检测某个端口是否占用
* @param host IP
* @param port 端口号
* @return 如果占用返回true;否则返回false
* @throws UnknownHostException IP地址不通或错误,则会抛出此异常
*/
public static boolean isPortUsing(String host, int port) throws UnknownHostException {
boolean flag = false;
InetAddress theAddress = InetAddress.getByName(host);
try{
Socket socket = new Socket(theAddress, port);
flag = true;
} catch (IOException e) {
//如果测试端口号没有占用,则会抛出异常,说明端口号没有被占用
}
return flag;
}