`

Java调用DOS命令

 
阅读更多
public static void main(String[] args) {
		  InputStream ins = null;
                       //&&可以执行多条命令
                //路径参数注意 要么\\ or /     
	      // String[] cmd = new String[] { "cmd.exe","/C","cd /d E:/1&&java com.stx.frame.TestCmd"};   
	       String cmd="cmd.exe /c dir";
	       try {
	       
	            Process process = Runtime.getRuntime().exec(cmd);
	             ins = process.getInputStream(); //cmd 的信息
	             
	             BufferedReader reader = new BufferedReader(new InputStreamReader(ins));   
	            
	             String line = null;   
	                while ((line = reader.readLine()) != null) {   
	                    System.out.println(line);  //输出 
	                } 
	                
	                int exitValue = process.waitFor();  
	                System.out.println("返回值:" + exitValue);  
	                process.getOutputStream().close(); //不要忘记了一定要关
	                
	        } catch (Exception e) {
	            
	            e.printStackTrace();
	        } 
	}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics