我正在使用运行时从我的Java程序运行命令提示符命令。但是我不知道如何获得命令返回的输出。java runtime.getruntime()从执行命令行程序获取输出
这里是我的代码:
Runtime rt = Runtime.getRuntime();
String[] commands = {"system.exe" , "-send" , argument};
Process proc = rt.exec(commands);
我想这样做System.out.print(proc);
但没有返回任何东西。该命令的执行应该返回两个以分号分隔的数字,我怎样才能得到这个变量打印出来?
这是我现在使用的代码:
String[] commands = {"system.exe","-get t"};
Process proc = rt.exec(commands);
InputStream stdin = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(stdin);
BufferedReader br = new BufferedReader(isr);
String line = null;
System.out.println("<OUTPUT>");
while ((line = br.readLine()) != null)
System.out.println(line);
System.out.println("</OUTPUT>");
int exitVal = proc.waitFor();
System.out.println("Process exitValue: " + exitVal);
但我没有得到任何东西作为我的输出,但是当我运行该命令自己正常工作。
1没有比该行多个Java - 运行时RT =调用Runtime.getRuntime(); – 2014-11-12 02:58:46
...除运行时Runtime runtime = Runtime.getRuntime(); :D – 2017-02-19 13:48:32