java调用windows系统进程并保存进程id,根据进程id杀死进程

在java开发中,有时候需要调用第三方的exe文件,同时又需要用java关闭此进程,或者通过进程id来写一个守护进程.这里是我参考网上总结的经验。兼容windowsserver服务器,网上有得是不兼容windowsserver的,因为传参和windows api给的参数不一致

code:

/**
** 执行cmd命令并获取进程pid值,可以自己保存在数据里面
**/
public static void start(String rtspUrl,String nginxUrl,String videoName){
        String all = "你的cmd命令启动第三方exe文件";
        Runtime runtime = Runtime.getRuntime();
        try {
            System.out.println(all);
            java.lang.Process process = runtime.exec(all);
            final Long pid = getPid(process);
            System.out.println(pid+"");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
/**
** 通过进程获进程id函数
**/
private static Long getPid(Process process){
        long handl = 0L;
        long pid = 0L;
        try {
            if (process != null) {
                Field f = process.getClass().getDeclaredField("handle");
                f.setAccessible(true);
                handl = f.getLong(process);
                Kernel32 kernel = Kernel32.INSTANCE;
                WinNT.HANDLE handle = new WinNT.HANDLE();
                handle.setPointer(Pointer.createConstant(handl));
                int ret = kernel.GetProcessId(handle);
                pid = Long.valueOf(ret);
            }
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return pid;
    }
/**
** 通过进程id杀死进程树,下面所有的进程又会被杀死
**/
private static void killProcessTree(Long pid)
    {
        try {
            String cmd =getKillProcessTreeCmd(pid);
            Runtime rt =Runtime.getRuntime();
            Process killPrcess = rt.exec(cmd);
            killPrcess.waitFor();
            killPrcess.destroy();
        }catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    private static String getKillProcessTreeCmd(Long Pid)
    {
        String result = "";
        if(Pid !=null)
            result ="cmd.exe /c taskkill /PID "+Pid+" /F /T ";
        return result;
    }
/**
** 通过进程id判断是否被杀死.可以通过这个方法写守护进程
**/
private static boolean isAlive(Long pid){
        Runtime runtime = Runtime.getRuntime();
        String queryCmd = "cmd /c tasklist|findstr "+pid;
        try {
            java.lang.Process process = runtime.exec(queryCmd);
            //取得命令结果的输出流
            InputStream fis = process.getInputStream();
            //用一个读输出流类去读
            InputStreamReader isr=new InputStreamReader(fis);
            //用缓冲器读行
            BufferedReader br=new BufferedReader(isr);
            String line=null;
            //直到读完为止
            if((line=br.readLine())!=null)
            {
                if(line.contains("你的进程名")){
                    return true;
                }
                else{
                    return false;
                }
            }
            else{
                return false;
            }
        }
        catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

rtmp转rtsp延迟,占用cpu问题,优化后得命令为

"cmd /k start ffmpeg  -re -hwaccel qsv -re  -rtsp_transport tcp -i \"rtsp://admin:[email protected]:554/Streaming/Channels/101\" -f flv  -vcodec copy -vprofile baseline -acodec aac -ar 24100 -ac 1 -f flv -s 720x360 -q 1 \"rtmp://127.0.0.1:1935/mylive/test1\"";

你可能感兴趣的:(java)