重启javafx App

时间:2018-03-02 07:23:33

标签: java javafx application-restart

我试图创建一个重启javafx应用程序的功能,在Windows 10上运行完美,但在Windows 7上它没有,我搜索这个解决方案并且它很完美,然后我测试它Windows 10什么也没有,该应用程序只是关闭。此外,我测试它在日志文件中查看异常并且它不会抛出任何异常。 必须制作具体的东西以便在Windows 7上工作?也许是另一种方法?感谢。

这是代码:

//Restart app the current Java application, with parameter you can pass a Runnable to do before restart app, null if not
public static void restartApplication(Runnable runBeforeRestart) throws IOException {
    try {
        /**
         * Sun property pointing the main class and its arguments.
         * Might not be defined on non Hotspot VM implementations.
         */
        final String SUN_JAVA_COMMAND = "sun.java.command";

        // java binary
        String java = System.getProperty("java.home") + "/bin/java";

        // vm arguments
        List<String> vmArguments = ManagementFactory.getRuntimeMXBean().getInputArguments();
        StringBuffer vmArgsOneLine = new StringBuffer();
        for (String arg : vmArguments) {
            // if it's the agent argument : we ignore it otherwise the
            // address of the old application and the new one will be in conflict
            if (!arg.contains("-agentlib")) {
                vmArgsOneLine.append(arg);
                vmArgsOneLine.append(" ");
            }
        }

        // init the command to execute, add the vm args
        final StringBuffer cmd = new StringBuffer("\"" + java + "\" " + vmArgsOneLine);

        // program main and program arguments
        String[] mainCommand = System.getProperty(SUN_JAVA_COMMAND).split(" ");

        // program main is a jar
        if (mainCommand[0].endsWith(".jar")) {
            // if it's a jar, add -jar mainJar
            cmd.append("-jar " + new File(mainCommand[0]).getPath());
        } else {
            // else it's a .class, add the classpath and mainClass
            cmd.append("-cp \"" + System.getProperty("java.class.path") + "\" " + mainCommand[0]);
        }

        // finally add program arguments
        for (int i = 1; i < mainCommand.length; i++) {
            cmd.append(" ");
            cmd.append(mainCommand[i]);
        }

        // execute the command in a shutdown hook, to be sure that all the
        // resources have been disposed before restarting the application
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                try {
                    Runtime.getRuntime().exec(cmd.toString());
                } catch (IOException e) {
                    logToFile.log(e, "info", "The application fail to restart, applying the command");
                }
            }
        });

        // execute some custom code before restarting
        if (runBeforeRestart!= null) {
            runBeforeRestart.run();
        }

        // Wait for 2 seconds before restart
        //TimeUnit.SECONDS.sleep(2);

        // exit
        System.exit(0);
    } catch (Exception e) {
        // something went wrong
        logToFile.log(e, "info", "The application fail to restart generally");
    }
}
  

更新:搜索其他方法我发现了一个解决方案,它在Windows操作系统上进行测试并且正常工作

这里是代码:

//Restart app the current Java application, with parameter you can pass a Runnable to do before restart app, null if not
public static void restartApplication(Runnable runBeforeRestart, Integer TimeToWaitToExecuteTask) throws IOException {
    try {

        // execute some custom code before restarting
        if (runBeforeRestart != null) {
            // Wait for 2 seconds before restart if null
            if (TimeToWaitToExecuteTask != null) {
                TimeUnit.SECONDS.sleep(TimeToWaitToExecuteTask);
            } else {
                TimeUnit.SECONDS.sleep(2);
            }
            runBeforeRestart.run();
        }

        final String javaBin = System.getProperty("java.home") + File.separator + "bin" + File.separator + "java";
        final File currentJar = new File(Main.class.getProtectionDomain().getCodeSource().getLocation().toURI());

        /* is it a jar file? */
        if (!currentJar.getName().endsWith(".jar"))
            return;

        /* Build command: java -jar application.jar */
        final ArrayList<String> command = new ArrayList<String>();
        command.add(javaBin);
        command.add("-jar");
        command.add(currentJar.getPath());

        final ProcessBuilder builder = new ProcessBuilder(command);
        builder.start();
        System.exit(0);

    } catch (Exception e) {
        // something went wrong
        logToFile.log(e, "info", "The application fail to restart generally");
    }
}

0 个答案:

没有答案