java runtime.getruntime()从执行命令行程序获取输出

我正在使用运行时从我的Java程序运行命令提示符命令。 但是我不知道如何得到命令返回的输出。

这是我的代码:

Runtime rt = Runtime.getRuntime(); String[] commands = {"system.exe" , "-send" , argument}; Process proc = rt.exec(commands); 

我尝试做System.out.print(proc); 但是没有任何回报。 该命令的执行应该返回两个用分号分隔的数字,我怎么能得到这个variables打印出来?

这里是我现在使用的代码:

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

但我没有得到任何东西作为我的输出,但是当我自己运行这个命令,它工作正常。

这是要走的路:

 Runtime rt = Runtime.getRuntime(); String[] commands = {"system.exe","-get t"}; Process proc = rt.exec(commands); BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream())); BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream())); // read the output from the command System.out.println("Here is the standard output of the command:\n"); String s = null; while ((s = stdInput.readLine()) != null) { System.out.println(s); } // read any errors from the attempted command System.out.println("Here is the standard error of the command (if any):\n"); while ((s = stdError.readLine()) != null) { System.out.println(s); } 

在这里更好地阅读Javadoc的更多细节。 ProcessBuilder将是不错的select

更快的方法是:

 public static String execCmd(String cmd) throws java.io.IOException { java.util.Scanner s = new java.util.Scanner(Runtime.getRuntime().exec(cmd).getInputStream()).useDelimiter("\\A"); return s.hasNext() ? s.next() : ""; } 

这基本上是这个简明的版本:

 public static String execCmd(String cmd) throws java.io.IOException { Process proc = Runtime.getRuntime().exec(cmd); java.io.InputStream is = proc.getInputStream(); java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A"); String val = ""; if (s.hasNext()) { val = s.next(); } else { val = ""; } return val; } 

我知道这个问题是古老的,但我发布这个答案,因为我认为这可能会更快。

除了使用ProcessBuilder作为build议Senthil之外,一定要阅读并实现Runtime.exec()的 所有build议。

@Senthil和@Arend的答案( https://stackoverflow.com/a/5711150/2268559 )提到了ProcessBuilder。 以下是使用ProcessBuilder为该命令指定环境variables和工作文件夹的示例:

  ProcessBuilder pb = new ProcessBuilder("ls", "-a", "-l"); Map<String, String> env = pb.environment(); // If you want clean environment, call env.clear() first // env.clear() env.put("VAR1", "myValue"); env.remove("OTHERVAR"); env.put("VAR2", env.get("VAR1") + "suffix"); File workingFolder = new File("/home/user"); pb.directory(workingFolder); Process proc = pb.start(); BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream())); BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream())); // read the output from the command System.out.println("Here is the standard output of the command:\n"); String s = null; while ((s = stdInput.readLine()) != null) { System.out.println(s); } // read any errors from the attempted command System.out.println("Here is the standard error of the command (if any):\n"); while ((s = stdError.readLine()) != null) { System.out.println(s); } 

改编自以前的答案

 public static String execCmdSync(String cmd, CmdExecResult callback) throws java.io.IOException, InterruptedException { RLog.i(TAG, "Running command:", cmd); Runtime rt = Runtime.getRuntime(); Process proc = rt.exec(cmd); //String[] commands = {"system.exe","-get t"}; BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream())); BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream())); StringBuffer stdout = new StringBuffer(); StringBuffer errout = new StringBuffer(); // read the output from the command System.out.println("Here is the standard output of the command:\n"); String s = null; while ((s = stdInput.readLine()) != null) { System.out.println(s); stdout.append(s); } // read any errors from the attempted command System.out.println("Here is the standard error of the command (if any):\n"); while ((s = stdError.readLine()) != null) { System.out.println(s); errout.append(s); } if (callback == null) { return stdInput.toString(); } int exitVal = proc.waitFor(); callback.onComplete(exitVal == 0, exitVal, errout.toString(), stdout.toString(), cmd); return stdInput.toString(); } public interface CmdExecResult{ void onComplete(boolean success, int exitVal, String error, String output, String originalCmd); } 

尝试阅读运行时的InputStream:

 Runtime rt = Runtime.getRuntime(); String[] commands = {"system.exe","-send",argument}; Process proc = rt.exec(commands); BufferedReader br = new BufferedReader( new InputStreamReader(proc.getInputStream())); String line; while ((line = br.readLine()) != null) System.out.println(line); } 

如果进程正在打印错误输出,则可能还需要读取错误stream( proc.getErrorStream() )。 如果使用ProcessBuilder则可以将错误streamredirect到inputstream。