使用JAVA执行命令行

java直接调用shell执行,各个操作系统都支持,下面的示例是调用windows cmd下的ipconfig

public class CallShell {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			Process process = Runtime.getRuntime().exec("ipconfig");
			InputStream is=process.getInputStream();
			byte[] b=new byte[1024];
			StringBuilder sb=new StringBuilder();
			while(is.read(b)>0){
				sb.append(new String(b));
			}
			System.out.println(sb.toString());
			
			int exitValue = process.waitFor();
			if (0 != exitValue) {
				System.out.println(exitValue);
			}
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}
}

猜你喜欢

转载自bucketli.iteye.com/blog/1254553