2016-05-17 57 views
1

我有一個Java程序,做一些實驗用的網絡:確保一個.BAT窗口關閉,完成後整個Java程序下山

javac *.java 
SET nodesize=11 
Set port=3001 
start rmiregistry 3300 & 
timeout /t 6 /nobreak > NUL 


start cmd /k java Node 3300 %port% %nodesize% 0 & 
timeout /t 1 /nobreak > NUL 
Set /A port=%port%+1 
start cmd /k java Node 3300 %port% %nodesize% 0 & 
timeout /t 1 /nobreak > NUL 
Set /A port=%port%+1 
start cmd /k java Node 3300 %port% %nodesize% 0 & 
timeout /t 1 /nobreak > NUL 

和使用核磁共振檢查註冊表,以其他連接的Java程序節點,然後它應該退出。

因此,這看起來是這樣的:

public static main(){ 
     Node n = new Node(); 
     n.notifyOthers();//informs others that a new node has joined 
     n.startAlgorithm(); 
} 

private synchronized void startAlgorithm() { 
    final NodeImplementation currentNode = this; 

    new Thread(new Runnable() { 
     public void run() { 
      try { 
       executeAlgorithm(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
       System.exit(0) 
      } 
     } 
    }).start(); 
} 

executeAlgorithm(){ 
     while(!done){ 
      //do a lot of stuff 
     //including messaging others 
     } 
     System.exit(); 
} 

但這並不關閉控制檯窗口,任何想法如何正確地做到這一點?

回答

2

我不完全理解你想用這個批處理文件實現什麼,以及爲什麼你把&符號放在最後(這不是Bash),但是你的窗口保持打開狀態的原因是你使用cmd /k

cmd /k將運行一個命令,然後保持窗口打開,顯示提示進一步命令。

cmd /c將運行一個命令後退出。

所以你需要將/k更改爲/c

您可以在控制檯中鍵入cmd /?以查看完整選項列表。

相關問題