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();
}
}
}