How to restart java program from java?

I have a java program that is quite large, and we want to make it so that the user can exit the application and log in as another user. To do this, we would like to close the application and restart it so that it presents the user with a login dialog. The problem is that the application is quite large and poorly written. It has many static variables that contain some status information. Ideally, I would like to rewrite the application in order to cope with a situation where all of them can be cleaned up, but in reality we should provide this functionality as soon as possible.

What I thought would be easiest to just stop the application and start a new vm. However, it is surprisingly difficult to stop and apply the application and run the same application, closing the current one. Does anyone have experience with this?

EDIT: we continue to use Runtime.exec to call the same application again, but exec () wants to block, so we need to jump over the hoops to make it work on every platform (Windows, Mac, Linux). I would prefer a platform-independent way of doing this.

+3
source share
4 answers

If you can change the code, perhaps you can exit the program and use the class Runtime( java.lang.Runtime) to run the same program again (with the same arguments?) Using the method exec().

http://download.oracle.com/javase/6/docs/api/java/lang/Runtime.html

: , , . , , (?).:)

+2

JVM , script (shell script , ), . System.exit(), , .

, , . , , . , : , System.exit() , , bootstrap.

+1

classloader: classname. . , , . , JBoss, " ".

0

You can use Runtime or ProcessBuilder to restart the application, but you probably have to change your application a bit, as I am sure that you have no way to get the full java executable from the JVM.

I suggest you run a launcher (as an executable or script) and use the java return code to find out if you need to exit or you need to exit or restart.

0
source

Source: https://habr.com/ru/post/1784579/


All Articles