Java runtime.getruntime()从执行命令行程序获得输出
我正在使用运行时从我的Java程序运行命令提示符命令。但是,我不知道如何获得命令返回的输出。
这是我的代码: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("");while ( (line = br.readLine()) != null)
System.out.println(line);System.out.println("");int exitVal = proc.waitFor();
System.out.println("Process exitValue: " + exitVal);
但是,我没有得到任何作为我的输出,但当我运行该命令自己,它工作良好。