I attempted to create a simplified failsafe Java application that would restart itself, whenever it was forcibly shutdown (in Windows, using CMD command of CTRL+C ).
The batch code looks like this :
@echo off
setlocal
start /wait java BatchWakeMeUpSomehow
if errorlevel 1 goto retry
echo Finished successfully
exit
:retry
echo retrying...
start /wait java BatchWakeMeUpSomehow
And the java code is this one :
public class WakeMeUpSomehow {
static class Message extends Thread {
public void run() {
try {
while(true)
{
System.out.println("Hello World from run");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
try {
Runtime.getRuntime().addShutdownHook(new Message());
while(true)
{
System.out.println("Hello World");
Thread.sleep(100);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
I wanted to clarify my understanding . I think that because Java runs within a JVM environment, then it has some limitations in regards to restarting a JVM which was forcibly closed down (if we restart a forcibly-shutdown JVM via a batch file, then it loses the cursor-focus and it just runs in a new CMD window that cannot have keyboard focus.
My goal was to have this app keep running in a loop (a la "digital wackamole" ), but I can only ever re-run it once (where it's running in a non-responsive CMD window )
Someone had suggested that I look at Apache Daemon tool, but I'm not sure how this helps.