How do I get rid of Java child processes when my Java app exits/crashes?

后端 未结 3 820
既然无缘
既然无缘 2020-12-10 12:23

I launch a child process in Java as follows:

final String[] cmd = {\"\"};
Process process = Runtime.getRuntime().exec(cmd);
         


        
3条回答
  •  囚心锁ツ
    2020-12-10 12:57

    I worked it out myself already. I add a shutdown hook, as follows:

    final String[] cmd = {""};
    final Process process = Runtime.getRuntime().exec(cmd);
    Runnable runnable = new Runnable() {
        public void run() {
            process.destroy();
        }
    };
    Runtime.getRuntime().addShutdownHook(new Thread(runnable));
    

提交回复
热议问题