java.lang.Runtime.exec的左膀右臂:流输入和流读取

来源:互联网 发布:看电子书的软件 编辑:程序博客网 时间:2024/06/15 01:00


java.lang.Runtime.exec()的左膀右臂:流输入和流读取

在java.lang.Runtime.exec的使用中,我们经常会用到将重定向命令执行的输入/结果或者将错误信息读取出来.

那么,在使用过程中,我们如何正确的使用呢?

什么是java.lang.Runtime

首先我们要明确一点,什么是Java.lang.Runtime? 我们来看官方[->link<-]的描述:

" Every Java application has a single instance of class Runtime that allows the application to interface with the environment in which the application is running. The current runtime can be obtained from thegetRuntime method.

An application cannot create its own instance of this class. "

也就是说,Runtime是每个java-application运行时有且仅有一个的当前实例.允许Application接入当前运行环境.

我们再来看看Runtime的exec()方法:

" Executes the specified  command in a separate process. "

这个方法可以让我们在一个进程中执行指定的命令.其返回类型是Process类.

那么我们还需要来看一下Process类:


什么是java.lang.Process

什么是Java.lang.Process? 我们来看官方[->link<-]的描述:

"The class Process provides methods for performing input from the process, performing output to the process, waiting for the process to complete, checking the exit status of the process, and destroying (killing) the process."

也就是说这个类提供控制线程的方法.

我们再来看看Process提供的获取输入流和输出流的方法:

  • public abstract InputStream getInputStream()

"Returns the input stream connected to the normal output of the subprocess.

The stream obtains data piped from the standard output of the process represented by thisProcess object."

  • public abstract OutputStream getOutputStream()

"Returns the output stream connected to the normal input of the subprocess.

Output to the stream is piped into the standard input of the process represented by thisProcess object."

  • public abstract InputStream getErrorStream()

"Returns the input stream connected to the error output of the subprocess.

The stream obtains data piped from the error output of the process represented by thisProcess object."

到这里,我们就明白里其中的因果>从exec()返回的Process的对象中调用获取流的方法.从而达到目的!


具体做法:

在需要使用exec()去执行命令并获取返回值的时候,具体的做法是什么呢?

仅仅围绕这个思路:"从exec()返回的Process的对象中调用获取流的方法getXStream"



              String s = null;              Process p = Runtime                      .getRuntime()                      .exec(                              new String[]{"/bin/sh",                                          "-c",                                          "java HelloWorld"},null,dir);              BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));              BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));              //打印出输出结果              log.info("标准输出命令");              while ((s = stdInput.readLine()) != null) {                    log.info(s);              }              log.info("标准错误的输出命令");              while ((s = stdError.readLine()) != null) {                    log.info(s);              }

其中

  • dir 是我的HelloWorld.class的存放目录,只能这样用,请参照:http://blog.csdn.net/timo1160139211/article/details/75006938
  • HelloWorld.java 的内容是Sysotem.out.println打印几行Hello World,此处没有编译,使用时应注意.


到此,大功告成!



推荐:

描写exec超棒的文章:用法和介绍很地道.

[link->]  When Runtime.exec() won't Navigate yourself around pitfalls related to the Runtime.exec() method

Java Runtime 详解:https://yq.aliyun.com/articles/48890

Java 魔法堂:找外援的利器--Runtime.exec详解:http://www.w2bc.com/Article/10201

参考资料:

Tutorialspoint : https://www.tutorialspoint.com/java/lang/java_lang_process.htm

Java Platform : http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html











原创粉丝点击