java在不同系统下关闭进程


前言

在不同系统的操作下,该如何关闭进程呢?
下面就让我们一起来看看吧!

window系统下:

package com.silin;

import java.io.IOException;
import java.lang.management.ManagementFactory;

public class Test06 {
    
    public static void main(String[] args){
        //获取系统类型
        String os = System.getProperty("os.name"); 
       //判断是系统类型    
        if(os.toLowerCase().startsWith("win")){         
          System.out.println(os);  
        //window系统
            String killCmd = "taskkill /f /im LogViewPro.exe";
            String killCmd1 = "taskkill /f /im wps.exe";
            Process p;
            try {
                p = Runtime.getRuntime().exec(killCmd);
                p = Runtime.getRuntime().exec(killCmd1);
                int runnngStatus = p.waitFor();
                System.out.println("已杀" + runnngStatus);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
        }
        }
    }
}


linux系统下:

package com.silin;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class CloseLinuxProcess {
    public static void main(String[] args) {
        String PID = getPID("java -jar test.jar");
        closeLinuxProcess(PID);
    }
    
    /**
     * 获取Linux进程的PID
     * @param command
     * @return
     */
    public static String getPID(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(Exception e){
            e.printStackTrace();
        }finally{
            if(reader!=null){
                try {
                    reader.close();
                } catch (IOException e) {
 
                }
            }
        }
        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(Exception e){
            e.printStackTrace();
        }finally{
            if(process!=null){
                process.destroy();
            }
            
            if(reader!=null){
                try {
                    reader.close();
                } catch (IOException e) {
 
                }
            }
        }
    }
}

代码是直接调用终端,对终端进行操作。因window与linux看看进程的方式不同。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容