确保.Bat窗口关闭,整个Java程序完成后关闭

时间:2016-05-17 07:14:53

标签: java windows batch-file cmd

我有一个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

使用MRI注册表与其他节点连接的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();
 }

但这并没有关闭控制台窗口,任何想法如何正确地做到这一点?

1 个答案:

答案 0 :(得分:2)

I don't fully understand what you are trying to achieve with that batch file, and why you put the ampersands at the end (this is not Bash), but the reason why your windows stay open is that you used cmd /k.

cmd /k will run a command and leave the window open afterwards, showing a prompt for further commands.

cmd /c will run a command and exit afterwards.

So you need to change /k to /c.

You can type cmd /? in the console to see a full list of options.