本文实例讲述了java简单实现调用 命令行 并获取执行结果。分享给大家供大家参考,具体如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
import java.io.bufferedreader; import java.io.inputstreamreader; public class command { public static void execmd(string commandstr) { bufferedreader br = null ; try { process p = runtime.getruntime().exec(commandstr); br = new bufferedreader( new inputstreamreader(p.getinputstream())); string line = null ; stringbuilder sb = new stringbuilder(); while ((line = br.readline()) != null ) { sb.append(line + "\n" ); } system.out.println(sb.tostring()); } catch (exception e) { e.printstacktrace(); } finally { if (br != null ) { try { br.close(); } catch (exception e) { e.printstacktrace(); } } } } public static void main(string[] args) { string commandstr = "ping www.baidu.com" ; //string commandstr = "ipconfig"; command.execmd(commandstr); } } |
运行结果:
希望本文所述对大家java程序设计有所帮助。
原文链接:https://blog.csdn.net/prstaxy/article/details/30050175
查看更多关于Java简单实现调用命令行并获取执行结果示例的详细内容...