Restart an Application in JavaFx

时光怂恿深爱的人放手 提交于 2019-12-05 10:07:05

This question lacks details. You did mention a JavaFX application but it is important to know how that application is being deployed. Is it running in the web browser, as a java webstart application, a stand-alone jar, or a self-contained native application? How are you starting the application to begin with? Having the answer to these questions will make it easier to answer your question with specifics.

While the following example is not JavaFX, the approach used here would work for some of the ways in which a JavaFX application can be deployed. One approach for restarting an application that works nicely is to start the application from a script. Inside the script would be a while loop that continually restarts the program based on the program exit code. Here is an example for a bash shell script which starts IntelliJ on a Linux platform:

while true ; do
   eval "$JDK/bin/java" $ALL_JVM_ARGS -Djb.restart.code=88 $MAIN_CLASS_NAME $*
   test $? -ne 88 && break
done

In this example, the startup script is passing "jb.restart.code" as an application parameter. If IntelliJ wishes to restart, it will return that value 88 as the exit code. The script observes the application exit code, and restarts the application if the value is 88.

This approach works well on most platforms, but requires the application to be initiated via a script.

One solution is to pass commandline and working dir in your start script to your main() method. Using ProcessBuilder you then can restart the appliation. Another possibility is to start the whole application in a custom classloader (e.g. Spring project has suitable classloaders in their source base), you then can basically restart by starting your main in anouther classloader, however you then need proper housekeeping to free threads and resources of the first instance.

retstart.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent t) {
            if(getOnCloseRequest()!=null){
                getOnCloseRequest().handle(new WindowEvent(getScene().getWindow(), WindowEvent.WINDOW_CLOSE_REQUEST));
//write code to invoke application instance again
            }else{
                close();
            }

        }
    });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!