cmd /k start 是指 启动cmd窗口,不关闭
cmd /c start 是指 启动cmd窗口,关闭
1 package com.laoxu.test.day02.executeShell; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 6 /** 7 * java调用批处理文件 8 * 9 */ 10 public class ShellTest { 11 private static final String FILE_CMD = "d:\\MongoDB\\MongoDBServerStart.bat"; 12 private static final String FILE_MONGO = "d:\\MongoDB\\mongo.exe"; 13 private static final String COMMAND_CMD = "cmd /k start "; // run.exec("cmd /k shutdown -s -t 3600"); 14 public static void main(String[] args) { 15 executeShell(); 16 } 17 public static void executeShell(){ 18 Runtime run = Runtime.getRuntime(); 19 try { 20 Process pro = run.exec(COMMAND_CMD+FILE_CMD); 21 run.exec(COMMAND_CMD+FILE_MONGO); 22 InputStream is = pro.getInputStream(); 23 while(is.read()!=-1){ 24 System.out.println(is.read()); 25 } 26 is.close(); 27 try { 28 pro.waitFor(); 29 } catch (InterruptedException e) { 30 e.printStackTrace(); 31 } 32 } catch (IOException e) { 33 e.printStackTrace(); 34 } 35 } 36 37 }