1

I'm building a restarter for my litte program.

The basic idea is to run a process using Runtime.getRuntime().exec, that excecutes the program itself, then running System.exit(0);

this is the code that does the restarting:

Runtime.getRuntime().addShutdownHook(new Thread() {
    @Override
    public void run() {
        try {
            Runtime.getRuntime().exec(strList);  
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
});

// exit
System.exit(0);

Where strList is an String[] that is something like:

  {"/bin/bash","-c","java -jar path/to.jar"}

Don't worry about OS portability, the program will be run exclusively on Ubuntu.

The program restarts fine, or at least a new process is running (and continuing to restart itself after a while)

The problem is after restart I loose the console, so i have no idea what is actually happening with the program.

Is there any way to start the new process in "the same window" or any way to access it (maybe by using screen?)?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Artog
  • 1,132
  • 1
  • 13
  • 25
  • 2
    I would go for a more solid solution, which is to build your application this way that it doesn't need to be restarted. – Martijn Courteaux Apr 12 '12 at 09:29
  • Its more about reducing maintence and ease updating – Artog Apr 12 '12 at 09:32
  • This is a duplicate question.. http://stackoverflow.com/questions/597927/how-to-open-a-command-terminal-in-linux – Phani Apr 12 '12 at 09:33
  • I suggest you use http://upstart.ubuntu.com/ instead of doing this with Java – artbristol Apr 12 '12 at 09:34
  • You're losing focus in your terminal because you started a new instance. I don't think there is a clean way to attach the fork to the same terminal session. – siebz0r Apr 12 '12 at 09:43

0 Answers0