如何清理Runtime.exec()启动的进程?

时间:2023-02-05 10:26:32

When my Java program is halted abnormally, applications started by Runtime.exec() do not stop. How do I stop these applications?

当我的Java程序异常停止时,Runtime.exec()启动的应用程序不会停止。如何停止这些应用程序?

Process process = null;
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable()
{
     public void run() {
          process.destroy();
     }
}));

try
{
     process = Runtime.getRuntime().exec("win.exe");
     process.waitFor();
}
catch (Exception e)
{
     e.printStackTrace();
}
finally
{
    process.destroy();
}

2 个解决方案

#1


0  

Worst case scenario: if you are using the program multiple times, in the next run you can kill that process.

最糟糕的情况:如果您多次使用该程序,在下一次运行中您可以终止该过程。

String command = "cmd /c taskkill /f /im win.exe";
Runtime.getRuntime().exec(command);

#2


0  

addShutdownHook does not execute on forced shutdown of Java such as:

addShutdownHook不会在强制关闭Java时执行,例如:

  • killing from Task Manager
  • 从任务管理器中查杀

  • killing via Eclipse Stop button
  • 通过Eclipse Stop按钮查杀

  • kill -9 on Linux.
  • 在Linux上杀死-9。

It does execute when using

它在使用时执行

  • Ctrl+c
  • other user initiated interruptions such as Windows shutdown.
  • 其他用户启动的中断,如Windows关机。

  • kill on Linux
  • 杀死Linux

You can test this by compiling your Java file into a class file, then running on the command line. After running on the command line, you can press Ctrl+c to observe that the shutdown hook is executed.

您可以通过将Java文件编译为类文件,然后在命令行上运行来测试它。在命令行上运行后,可以按Ctrl + c来观察执行关闭挂钩。

To test, create RunTimeTest.java as shown below, then compile using your JDK.

要测试,请创建RunTimeTest.java,如下所示,然后使用JDK进行编译。

For example:

"c:\Program Files\Java\jdk1.7.0_40\bin"\javac RunTimeTest.java
"c:\Program Files\Java\jdk1.7.0_40\bin"\java RunTimeTest

Test code:

import java.io.IOException;
import java.util.Arrays;

public class RunTimeTest {

    public static void main(String arguments[]) {
        ProcessBuilder processBuilder = new ProcessBuilder(Arrays.asList("notepad.exe"));

        Process process = null;
        try {
            process = processBuilder.start();
            final Process processToKill = process;
            Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println("Shutdown Hook");
                    processToKill.destroy();
                }
            }));
            process.waitFor();
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        } finally {
            if (process != null) {
                System.out.println("End of try");
                process.destroy();
            }
        }
    }
}

See Javadoc for Runtime

请参阅Javadoc for Runtime

#1


0  

Worst case scenario: if you are using the program multiple times, in the next run you can kill that process.

最糟糕的情况:如果您多次使用该程序,在下一次运行中您可以终止该过程。

String command = "cmd /c taskkill /f /im win.exe";
Runtime.getRuntime().exec(command);

#2


0  

addShutdownHook does not execute on forced shutdown of Java such as:

addShutdownHook不会在强制关闭Java时执行,例如:

  • killing from Task Manager
  • 从任务管理器中查杀

  • killing via Eclipse Stop button
  • 通过Eclipse Stop按钮查杀

  • kill -9 on Linux.
  • 在Linux上杀死-9。

It does execute when using

它在使用时执行

  • Ctrl+c
  • other user initiated interruptions such as Windows shutdown.
  • 其他用户启动的中断,如Windows关机。

  • kill on Linux
  • 杀死Linux

You can test this by compiling your Java file into a class file, then running on the command line. After running on the command line, you can press Ctrl+c to observe that the shutdown hook is executed.

您可以通过将Java文件编译为类文件,然后在命令行上运行来测试它。在命令行上运行后,可以按Ctrl + c来观察执行关闭挂钩。

To test, create RunTimeTest.java as shown below, then compile using your JDK.

要测试,请创建RunTimeTest.java,如下所示,然后使用JDK进行编译。

For example:

"c:\Program Files\Java\jdk1.7.0_40\bin"\javac RunTimeTest.java
"c:\Program Files\Java\jdk1.7.0_40\bin"\java RunTimeTest

Test code:

import java.io.IOException;
import java.util.Arrays;

public class RunTimeTest {

    public static void main(String arguments[]) {
        ProcessBuilder processBuilder = new ProcessBuilder(Arrays.asList("notepad.exe"));

        Process process = null;
        try {
            process = processBuilder.start();
            final Process processToKill = process;
            Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println("Shutdown Hook");
                    processToKill.destroy();
                }
            }));
            process.waitFor();
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        } finally {
            if (process != null) {
                System.out.println("End of try");
                process.destroy();
            }
        }
    }
}

See Javadoc for Runtime

请参阅Javadoc for Runtime