I'm trying to get my Java application to be able to restart itself programmatically.
I already have a working solution for when it's executed as a standalone application - the application invokes java.exe with its own classpath, jvm parameters, class name, and command line arguments; and then it exits (see my stack overflow answer).
But this approach doesn't work cleanly when the application is run as a windows service - instead of a restarted service, I end up with a java.exe process executed by the SYSTEM account, and the state of the service is stopped (since it exited). So the application does restart, but is no longer a service.
I know that windows restarts services that exit with a non-zero exit code but a) I'm not sure that it always works (see this question) b) I'm not sure how to identify the fact that the application is running as a windows service in the first place - System.getProperty("user.name")
just seemed to return the hostname followed by a dollar sign ($) in my tests.
Can anyone suggest a solution?