java方法执行控制台命令工具类

package com.yanek.util;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;

public class Tool {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		System.out.println(executeCommond("ipconfig"));

	}
     /**
      *  java方法执行控制台命令,返回执行的输入结果
      * @param cmd
      * @return
      */
	public static String executeCommond(String cmd) {
		String ret = "";
		try {
			Process p = Runtime.getRuntime().exec(cmd);
			InputStreamReader ins = new InputStreamReader(p.getInputStream());
			LineNumberReader input = new LineNumberReader(ins);

			String line;
			while ((line = input.readLine()) != null) {
				System.out.println(line);
				ret += line + "<br>";
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return ret;
	}

}


 

上一篇:JVM 看不到某些异常的stacktrace问题(转)


下一篇:java.net.SocketException: Unrecognized Windows Sockets error: 0: JVM_Bind 处理方法