发送Ctrl+;C转换为java中启动的进程

发送Ctrl+;C转换为java中启动的进程,java,process,cmd,pid,Java,Process,Cmd,Pid,我希望能够终止从java程序开始的ping,但我需要获得pid才能这样做。我也做了一些研究,我发现我可以使用发送Ctl+C,但我需要刚刚开始的进程的pid。Java中有没有方法获取pid Process p = null; InputStream processOutput; BufferedReader reader = null; String line = " "; p = Runtime.getRuntime().exec("cmd /c " +

我希望能够终止从java程序开始的ping,但我需要获得pid才能这样做。我也做了一些研究,我发现我可以使用发送Ctl+C,但我需要刚刚开始的进程的pid。Java中有没有方法获取pid

    Process p = null;
    InputStream processOutput;
    BufferedReader reader = null;
    String line = " ";

    p = Runtime.getRuntime().exec("cmd /c " + command);
    processOutput = p.getInputStream();
    reader = new BufferedReader(new InputStreamReader(processOutput));
    // Read the input
    while((line = reader.readLine()) != null){
        output += line + "\n";
        System.out.println(line);

    }

exec(…)返回一个进程对象。如果您调用process.destroy(),会有帮助吗?

谢谢。这对我很有帮助。不知道为什么我在谷歌搜索时没有看到它。不用担心。我也做过很多次了,这不是一个重复的问题,因为这个问题是关于子进程的PID,而不是当前进程的PID。