Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
441 views
in Technique[技术] by (71.8m points)

java - Restart Swing Application

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...