I want to close and then restart a already running application (automatically) that I am making, by clicking a button or something like that, I want to do that for the purpose of re-launching the application in an other language, I'm new to JavaFx and Java in general, please can you give me a solution for this problem ?
-
1Yes your question is about it. But why do you need to restart the app to change the language? Maybe it can be changed without re-launching? What is your app's language configuration? – Uluk Biy Jun 15 '13 at 22:53
-
Well I don't know much about these mechanisms, what I want is relaunch the app to redefine all the objects/controls in the other language – AymenDaoudi Jun 15 '13 at 23:14
-
Check @UlukBiy's solution to [JavaFX 2 and Internationalization](http://stackoverflow.com/questions/10143392/javafx-2-and-internationalization) question. – Salah Eddine Taouririt Jun 17 '13 at 19:51
-
@UlukBiy I'd like to know how to switch language without restart the application, if the ResourceBundle is loaded via FXMLLoader.load(). Thanks! – wceo Nov 07 '13 at 09:26
-
@wceo, see [JavaFX 2 and Internationalization](http://stackoverflow.com/questions/10143392/javafx-2-and-internationalization) – Uluk Biy Nov 07 '13 at 16:50
3 Answers
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.

- 4,972
- 8
- 44
- 73
-
-
If it is a native self-contained application, then which OS is it native to? These details make a difference in what the answer would be. – axiopisty Apr 10 '15 at 21:57
-
Currently I implemented the Windows solution via exec followed by System.exit(). I think Linux and MacOS will follow the same path with the relative shell commands. – Claudio Mezzasalma Apr 10 '15 at 22:08
-
Are you sure you're using a native self-contained application? It sounds to me that if you're using a script to start the application, you're probably running a stand alone jar file on windows. If you're doing that then the script solution will work for you. My understanding is that a native self-contained application installs on the OS through either an exe or msi file. You might still be able to find a similar solution for this approach depending on how your native application is packaged, deployed, and started on the native platform. – axiopisty Apr 10 '15 at 22:16
-
Absolutely sure. JavaFX self-contained applications can be deployed in native bundles (.exe for Windows, .app for Mac OS X, executable for Linux) in which a native bootstrapper that launches the main JAR is created. – Claudio Mezzasalma Apr 11 '15 at 05:40
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.

- 3,436
- 1
- 17
- 12
-
Well, I'm actually very new to this, and I'm kinda confused and not getting your answer, can you please show me some code that may help or refer to an article explaining that, thx – AymenDaoudi Jun 15 '13 at 22:26
-
Well actually the Classloader thingy is pretty complicated, better try the command line approach. You can start an OS process from Java, so all you need is to know where the java executable is located and how the classpath is set. You can find about the classpath using System.getProperty("java.class.path") or similar. The location of the java.exe you have to pass to your application at startup time. Using Runtime.getRuntime().exec(..) or just ProcessBuilder will then let you start a complete copy of your application. – R.Moeller Jun 15 '13 at 22:35
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();
}
}
});

- 195
- 10