重新启动Swing应用程序

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

是否有其他以Java为中心的方法来实现此功能?是否可以只派生另一个副本然后退出?

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


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

@mKorbel我写的,采取的概念下面的代码,你曾在所示这样

    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,该应用程序将完全终止,即不再再次启动。

非常感谢您的帮助。


解决了。

回答:

解决方案:

从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);

谢谢。

以上是 重新启动Swing应用程序 的全部内容, 来源链接: utcz.com/qa/403255.html

回到顶部