为什么我不能从异常块中重新执行 Java 程序?

Why can't I re-execute a Java program from within an Exception block?

我正在尝试 运行 以下 Java 代码,当我在 windows 命令行上通过 CTRL + C 杀死它时,它应该会自动重启:

import java.net.*;
import java.io.*;

public class LineRunner extends Thread {

    public static void main(String[] args) throws InterruptedException, IOException{

        try {

          for (int i = 0; i<10000000; i++) {
          Thread.sleep(200);
          System.out.print("hithe");
        }
        }
        catch(  InterruptedException ioex) {

             String[] command = {"C://Program Files//Java//jdk1.7.0_02//bin//java.exe", "LineRunner"};  
             ProcessBuilder pb = new ProcessBuilder(command);
             pb.redirectErrorStream(true);
             Process exec = pb.start();

             BufferedReader br = new BufferedReader(new InputStreamReader(exec.getInputStream()));
             String text = null;
             while ((text = br.readLine()) != null) {
                 System.out.println(text);
             }

             System.out.println("Process exited with " + exec.waitFor());

        }
    }
}

但是当我从命令行(通过 CTRL + C )终止时,它并没有像我希望的那样重新启动程序。

感谢任何提示,谢谢

Ctrl-C 将退出 JVM,如评论中所述。

您需要使用addShutdownhook来监听程序退出,您可以在那里重新启动您的程序。