tags:

views:

487

answers:

3

Can a Java.exe JVM restart itself somehow? If you have a Java program running in a DOS shell (NOT as a service), what is the cleanest, most efficient way for it to shut down, close the shell, and restart itself with a different Windows process id and re-open in a different shell with a reloaded classpath?

I expect it's not possible but I thought I should ask and learn from other people by asking.

NOTE: If anyone knows how Eclipse IDE is able to restart itself so cleanly, that is the method I am wanting to use.

+1  A: 

The cleanest and simplest way to do that would be to run the program from a DOS batch file (or CMD script, if you prefer). Build a loop into your DOS batch file. You put a :label near the beginning and a GOTO label below your Java process and off it goes. If need be, you can break out of the loop using Ctrl-C.

Carl Smotricz
hmmm... you give me an interesting idea... if I can somehow inject a fatal error to the JVM then i could take advantage of the -XX:OnError="<cmd args>;<cmd args>" option to run a batch file after the jvm dies... but that would still leave the old DOS console open. not very clean.
djangofan
That won't do anything that your shutdown hook can't do.
Carl Smotricz
+3  A: 

The Java Service Wrapper can help here. You can run your Java process under it but not necessarily as a service. You can define restart characteristics, such that if it exits, the Java program is restarted (either by System.exit() or calling WrapperManager.restart()).

Brian Agnew
in my question I specifically said "not running as a service". i currently use this wrapper. im really looking for a way to do this without a formal wrapper of any kind. in my question , i am thinking of the DOS shell as being the only wrapper...
djangofan
Well, you can use this wrapper *without* running as a service
Brian Agnew
hmm... interesting idea. i didn't think of that... can it run in the foreground as a interactive DOS shell within the wrapper?
djangofan
Well, it runs as a .exe. So you can run it within a shell
Brian Agnew
I've used JavaService wrapper for years. It won't do as I need, as I said earlier. I specifically said in this question that the answer I am looking for DOES NOT involve running as a service.
djangofan
Well, you *don't* need to run it as a service. You can simply run it as a wrapper around what you currently run. I've said explicitly twice that it doesn't have to be a service.
Brian Agnew
A: 

Create a separate executable that takes a process ID and a command as command line argument, waits for the specified process to close and then runs the specified command. Launch that program, then exit the current instance.

You can either embed that program in your primary program and copy it to a temporary directory to execute it, or include it side-by-side with your program.

280Z28
this is how i currently do it: use java to start the shutdown of the java service then at the end use java (programatically) to start the second service in a new shell. the problem is that it leaves a commmand console sitting there after the first jvm shuts down.
djangofan