2

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.

Community
  • 1
  • 1
Tapas Bose
  • 25,780
  • 71
  • 202
  • 317

2 Answers2

3

The solution:

Call the following code from the 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);

Thanks.

Tapas Bose
  • 25,780
  • 71
  • 202
  • 317
0

The link that u provided in your answer is the best way to restart your swing app. Let me ask you, is not having it done through " WORA" a bad thing? Most applications come with a platform centric launcher. It makes your app look and feel like a native application. Are u planning on deploying your entire app as a double clickable jar file ? Double clicking a jar file is not very intuitive. Sometimes if the local computer installs winip or winrar for example if you double click a jar file it doesn't launch your app by default.

To solve such issues it's best having a native launcher. Take a look at db visualizer it has native launchers for it's java swing program

sethu
  • 7,407
  • 5
  • 35
  • 61