0

I'm trying to run the following Java code which is supposed to automatically restart itself when I kill it via CTRL + C on windows command-line :

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());

        }
    }
}

But when I kill from command line (via CTRL + C ) , it does not restart the program as I wish.

Any tips appreciated, thanks

Caffeinated
  • 11,982
  • 40
  • 122
  • 216
  • 1
    Any exception? If so, please post the stacktrace. Furthermore please format your source code properly. – Turing85 Jul 19 '15 at 03:22
  • 2
    Do you know that Ctrl-C causes an interrupt exception to be thrown? I thought that it shuts down the JVM (but could be wrong). Also, what is the rationale behind this code? It does not look like a safe program design, and if it worked could prove to be extremely annoying. – Hovercraft Full Of Eels Jul 19 '15 at 03:23
  • @HovercraftFullOfEels - I believe you may be right about that point. When I type `CTRL + C` it simply stops outputting and goes to the next command line – Caffeinated Jul 19 '15 at 03:25
  • @HovercraftFullOfEels - Confirmed, you' are correct. CTRL+C kills the JVM , according to some search results - https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#safe=off&q=%22ctrl%2Bc+will+kill+the+jvm%22 – Caffeinated Jul 19 '15 at 03:31
  • Hmm, maybe my question has an impossibility in it then. I'm suspecting that you *cannot* rely on the specific Java class to ensure it gets restarted. Will look again at batch file – Caffeinated Jul 19 '15 at 03:33
  • @HovercraftFullOfEels - Well, the point is mainly just as a proof of concept ( academic exercise) - I want a program that demonstrates fail-safe ability. So that if killed via `CTRL + C` it restarts, infinitely. – Caffeinated Jul 19 '15 at 03:35
  • 2
    It sounds like you want to trap SIGINT. This [answer](http://stackoverflow.com/questions/2541475/capture-sigint-in-java) may give you some ideas. – Rusty Shackleford Jul 19 '15 at 03:36
  • 2
    Again though this program design has the potential to be extremely annoying to the user. – Hovercraft Full Of Eels Jul 19 '15 at 03:39
  • @HovercraftFullOfEels - So, you're confident it can be implemented though? IE with the SIGINT ? I tried but still no luck – Caffeinated Jul 19 '15 at 11:38

2 Answers2

2

You need use addShutdownhook to listen the program exit and you can restart your program in there.

chengpohi
  • 14,064
  • 1
  • 24
  • 42
0

Ctrl-C will exit the JVM as has already been discussed in comments.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 1
    Made a community wiki so that I (or plkmthr) don't get points for something discussed previously in comments. Especially since plkmthr answered *after* this had all been well established in comments. – Hovercraft Full Of Eels Jul 19 '15 at 03:37
  • I didn't see the comments till I came today and checked the negative vote on the answer. It is difficult to keep track of comments and answers on the stack overflow app. I never downvote anyone. When you get downvoted for no reason, it makes no sense. If you downvoted me because you think you already established something which the world already knows than you make no sense to me either. – systemhalted Jul 24 '15 at 03:57