I am willing to add a button in my application which on-click will restart the app. I searched Google but found nothing helpful except this one. But the procedure follows here is violating the WORA concept of Java.
Is there any other Java centric way to achieve this feature? Is it possible to just fork another copy and then exit?
Thanks in advance. I appreciate your help.
@deporter I have tried your solution but it is not working :(
@mKorbel I wrote the following code by taking concept you had shown in 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);
}
});
And also:
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);
its working but only once. If I launch the application for first time and press JYM menuitem then it shutdowns and after few second it opens a new ui with cmd, but if I press that JYM menuitem the application terminate completely, i.e., it is not again launches anymore.
I really appreciate your help.
It is solved.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…