如何将Commons Exec运行的信息捕捉呢?
下文笔者讲述使用java代码获取Commons Exec运行信息的方法分享,如下所示
实现思路: 只需使用PumpStreamHandler对象接收命令执行的输出流 即可获取相关信息例:
import java.io.ByteArrayOutputStream; import org.apache.commons.exec.CommandLine; import org.apache.commons.exec.DefaultExecutor; import org.apache.commons.exec.Executor; import org.apache.commons.exec.PumpStreamHandler; public String execToString(String command) throws Exception { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); CommandLine commandline = CommandLine.parse(command); DefaultExecutor exec = new DefaultExecutor(); PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream); exec.setStreamHandler(streamHandler); exec.execute(commandline); return(outputStream.toString()); }
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。