首页 > 代码库 > JAVA中调用CMD命令,并输出执行结果

JAVA中调用CMD命令,并输出执行结果

package com.wzw.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class CmdDemo {

		public static void main(String[] args) {
			BufferedReader br = null;
			try {
				Process p = Runtime.getRuntime().exec("net user");
				br = new BufferedReader(new InputStreamReader(p.getInputStream()));
				String line = null;
				while ((line = br.readLine()) != null) {
					System.out.println(line);
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {

				if (br != null) {
					try {
						br.close();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}
	}



执行结果:


\\PC-20140327DFCW 的用户帐户

-------------------------------------------------------------------------------
Administrator            Guest                    
命令成功完成。