2012-02-22 197 views
2

我願意在我的應用程序中添加一個按鈕,點擊後將重新啓動應用程序。我搜索谷歌,但發現沒有什麼有用的,除了this one。但是這裏的程序違背了WORA的Java概念。重新啓動Swing應用程序

是否有任何其他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菜單項然後關閉,幾秒鐘後它會打開一個帶有cmd的新UI,但是如果我按下該JYM菜單項,則應用程序將完全終止,即不會再次啓動。

我真的很感謝你的幫助。


它被解決了。

+0

只是好奇,什麼是java的WORA概念? – 2012-02-22 07:10:12

+0

這可能幫助:http://stackoverflow.com/questions/4159802/how-can-i-restart-a-java-application – MByD 2012-02-22 07:11:39

+0

@HarryJoy - http://en.wikipedia.org/wiki/Write_once,_run_anywhere – MByD 2012-02-22 07:12:05

回答

3

解決辦法:

呼叫從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); 

謝謝。

0

,美在你的答案中提供的鏈接是重新啓動您的Swing應用程序的最佳途徑。讓我問你,是不是通過「WORA」完成了一件壞事?大多數應用程序都帶有一個以平臺爲中心的啓動器。它使您的應用看起來像一個本地應用程序。您是否計劃將整個應用程序部署爲可雙擊的jar文件?雙擊jar文件不太直觀。有時如果本地計算機安裝winip或winrar,例如,如果你雙擊一個jar文件,它默認不啓動你的應用程序。

爲了解決這些問題,最好有本機啓動。看看分貝可視化有本地發射了它的Java程序擺動