一 Linux
public static void main(String[] args) {
StringPID=getPID("java -jar test.jar");
closeLinuxProcess(PID);
}
/**
* 获取Linux进程的PID
* @param command
* @return
*/
public static StringgetPID(String command){
BufferedReader reader=null;
try{
//显示所有进程
Process process = Runtime.getRuntime().exec("ps -ef");
reader =new BufferedReader(new InputStreamReader(process.getInputStream()));
String line =null;
while((line = reader.readLine())!=null){
if(line.contains(command)){
System.out.println("相关信息 -----> "+command);
String[] strs = line.split("\\s+");
return strs[1];
}
}
}catch(Exceptione){
e.printStackTrace();
}finally{
if(reader!=null){
try {
reader.close();
}catch (IOExceptione) {
}
}
}
return null;
}
/**
* 关闭Linux进程
* @param Pid 进程的PID
*/
public static void closeLinuxProcess(String Pid){
Process process =null;
BufferedReader reader=null;
try{
//杀掉进程
process = Runtime.getRuntime().exec("kill -9 "+Pid);
reader =new BufferedReader(new InputStreamReader(process.getInputStream()));
String line =null;
while((line = reader.readLine())!=null){
System.out.println("kill PID return info -----> "+line);
}
}catch(Exceptione){
e.printStackTrace();
}finally{
if(process!=null){
process.destroy();
}
if(reader!=null){
try {
reader.close();
}catch (IOExceptione) {
}
}
}
}
二 Windows
/**
* @desc 启动进程
* @author zq
*/
public static void startProc(String processName) {
log.info("启动应用程序:" + processName);
if (StringUtils.isNotBlank(processName)) {
try {
Desktop.getDesktop().open(new File(processName));
} catch (Exception e) {
e.printStackTrace();
log.error("应用程序:" + processName + "不存在!");
}
}
}
/**
* @desc 杀死进程
* @author zq
* @throws IOException
*/
public static void killProc(String processName) throws IOException {
log.info("关闭应用程序:" + processName);
if (StringUtils.isNotBlank(processName)) {
executeCmd("taskkill /F /IM " + processName);
}
}
/**
* @desc 执行cmd命令
* @author zq
*/
public static String executeCmd(String command) throws IOException {
log.info("Execute command : " + command);
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("cmd /c " + command);
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8"));
String line = null;
StringBuilder build = new StringBuilder();
while ((line = br.readLine()) != null) {
log.info(line);
build.append(line);
}
return build.toString();
}
/**
* @desc 判断进程是否开启
* @author zq
*/
public static boolean findProcess(String processName) {
BufferedReader bufferedReader = null;
try {
Process proc = Runtime.getRuntime().exec("tasklist -fi " + '"' + "imagename eq " + processName +'"');
bufferedReader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line = null;
while ((line = bufferedReader.readLine()) != null) {
if (line.contains(processName)) {
return true;
}
}
return false;
} catch (Exception ex) {
ex.printStackTrace();
return false;
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (Exception ex) {}
}
}
}
//downfile = "D:\\DownFile\\DownFile.exe";
String url = request.getParameter("downfile");
String procName = url.substring(url.lastIndexOf("\\")+1,url.length());
boolean exist= findProcess(procName);
try {
if (exist) {
// 存在,那么就先杀死该进程
killProc(procName);
// 在启动
startProc(url);
}else {
startProc(url);
}
} catch (Exception e) {
// TODO: handle exception
log.error("重启/杀死提取程序失败。。。");
}
网友评论