【问题标题】:Restart Swing Application重新启动 Swing 应用程序
【发布时间】:2012-02-22 07:06:26
【问题描述】:

我愿意在我的应用程序中添加一个按钮,单击该按钮将重新启动应用程序。我搜索了谷歌,但发现除了 this one 之外没有任何帮助。但是这里遵循的过程违反了 Java 的 WORA 概念。

还有其他以 Java 为中心的方式来实现此功能吗?是否可以只分叉另一个副本然后退出?

提前致谢。感谢您的帮助。


@deporter 我已经尝试过您的解决方案,但它不起作用:(

@mKorbel 我根据您在so 中显示的概念编写了以下代码

    JMenuItem jMenuItem = new JMenuItem("JYM");
    jMenuItem.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

            ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(10);
            executor.setContinueExistingPeriodicTasksAfterShutdownPolicy(true);
            executor.setExecuteExistingDelayedTasksAfterShutdownPolicy(true);
            ScheduledFuture<?> future = executor.schedule(new Runnable() {

                @Override
                public void run() {
                    try {
                        Process p = Runtime.getRuntime().exec("cmd /c start java -jar D:\\MovieLibrary.jar");
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }
            }, 2, TimeUnit.SECONDS);                
            executor.shutdown();
            try {
                executor.awaitTermination(10, TimeUnit.SECONDS);
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }

            System.exit(0);
        }
    });

还有:

ScheduledExecutorService schedulerExecutor = Executors.newScheduledThreadPool(2);
            Callable<Process> callable = new Callable<Process>() {

                @Override
                public Process call() throws Exception {
                    Process p = Runtime.getRuntime().exec("cmd /c start java -jar D:\\MovieLibrary.jar");
                    return p;
                }
            };
            FutureTask<Process> futureTask = new FutureTask<Process>(callable);
            schedulerExecutor.submit(futureTask);
            schedulerExecutor.shutdown();
            try {
                schedulerExecutor.awaitTermination(10, TimeUnit.SECONDS);
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }

            System.exit(0);

它的工作,但只有一次。如果我第一次启动应用程序并按下 JYM menuitem 然后它会关闭并在几秒钟后使用 cmd 打开一个新的 ui,但如果我按下该 JYM menuitem 应用程序完全终止,即它不再启动。

非常感谢您的帮助。


解决了。

【问题讨论】:

标签: java swing


【解决方案1】:

解决办法:

从 ActionListener 调用以下代码。

ScheduledExecutorService schedulerExecutor = Executors.newScheduledThreadPool(2);
Callable<Process> callable = new Callable<Process>() {

    @Override
    public Process call() throws Exception {
        Process p = Runtime.getRuntime().exec("cmd /c start /b java -jar D:\\MovieLibrary.jar");
        return p;
    }
};
FutureTask<Process> futureTask = new FutureTask<Process>(callable);
schedulerExecutor.submit(futureTask);           

System.exit(0);

谢谢。

【讨论】:

    【解决方案2】:

    您在答案中提供的链接是重新启动 Swing 应用程序的最佳方式。让我问你,通过“WORA”完成它不是一件坏事吗?大多数应用程序都带有以平台为中心的启动器。它使您的应用程序看起来和感觉就像一个本机应用程序。您是否打算将整个应用程序部署为可双击的 jar 文件?双击 jar 文件不是很直观。有时,如果本地计算机安装了 winip 或 winrar,例如,如果您双击 jar 文件,默认情况下它不会启动您的应用程序。

    要解决此类问题,最好使用本机启动器。看看 db Visualizer,它的 java swing 程序有本机启动器

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-14
      • 2011-03-08
      相关资源
      最近更新 更多