Search code examples
javaeclipserestart

Java - Restart a program running in eclipse


So, basically, I have a JComboBox which I use to change the current language of the app. To achieve this, since I have a lot of components and I don't really want to manually change the text on all of them (buttons, ...), I think the best solution is to just restart the program with a "-language "XX"" argument (which I can easily handle on the program start-up method).

This code does the job:

public static void restartApplication(String language)
{
    String javaBin = System.getProperty("java.home") + File.separator + "bin" + File.separator +      "java";
    File currentJar = new     File(Main.class.getProtectionDomain().getCodeSource().getLocation().toURI());

    // Build command: java -jar application.jar -language "EN" 
    String command = "\"";
    command += javaBin + "\"" +
        " -jar " + "\"" + currentJar.getPath() + "\"" +
        " -language \"" + language + "\"";

    // Execute command
    new ProcessBuilder(command).start();

    // Close the current instance
    System.exit(0);
}

My problem is, well, how to make it work in an IDE (Eclipse, in this case)? Do I need to locate the program's main class (compiled by Eclipse?)?

Edit: Actually, designing it in a way that I don't have to restart my program would be better. Thanks!


Solution

  • I know I do not answer directly to your question about how to restart a program running eclipse because you shouldn't have to restart your program to change the language.

    Like aioobe said here:

    I recommend you to design your application so that it is easy to clean every thing up and after that create a new instance of your "main" class.

    You should designed your application to do nothing but create an instance in the main-method.

    public static void main(String[] args) {
        boolean restart;
    
        do {
            restart = new MainClass().switchLang();
        } while (restart);
    }
    

    and let switchLang() return true if you change your language.