processbuilder - JAVA做操作系统的进程管理,除了Commons Exec外,还有其它好的开源包吗
阿神
阿神 2017-04-17 15:57:33
0
1
505

我要用JAVA程序去管理系统进程,包括启动一个应用程序、判断一个应用程序的运行状况、结束一个应用程序。
该程序需要在Windows/Linux/Unix下运行。

Apache有一个Commons Exec包,是做这个事情的。

请问,还有什么更优秀的开源软件可选吗?

阿神
阿神

闭关修行中......

reply all(1)
Ty80

I just took a look at the documentation of Commons Exec, and there is another solution to the pipeline problem you mentioned. The pipeline actually just connects the output stream of the previous program to the input stream of the following program. I read the documentation and found that Commons Exec provides an interface for redirecting process input and output streams. Commons Exec的文档,你所说的管道问题还有另外一个解决办法。管道实际上只是把前一个程序的输出流和后一个程序的输入流连接起来而已。我看了文档,Commons Exec提供了重定向进程输入输出流的接口。

具体来说就是,假设有A、B两个程序,原本是通过管道命令连接在一起的(A | B)。用Commons Exec可以这样做:

先创建一个运行A程序的Executor对象,然后获取它的ExecuteStreamHandler(通过getStreamHandler()方法),重设该对象的输出流为你自己定义的一个输入流(标准输入或文件都可以)。(注意A程序的输出流实际上是一个输入流对象,即InputStream对象,例如标准输入。这是因为程序的输出类似于我们向标准输入打字一样,所以其作用目标是一个输入流)

这样就相当于你拿到了A程序的输出流,现在的关键是把这个输出流赋给B程序的输入流。

你可以自己把前面得到的A程序的输出流(一个InputStream对象)转换为一个OutputStream对象(很简单,从前者读取后直接向后者写入即可),然后按照与A程序相同的处理方式,把这个输出流对象赋给B程序的Executor

Specifically, suppose there are two programs A and B, which were originally connected through pipeline commands (A | B). You can do this with Commons Exec:

First create an Executor object that runs program A, then obtain its ExecuteStreamHandler (through the getStreamHandler() method), and reset the object's The output stream is an input stream you define (either standard input or a file). (Note that the output stream of program A is actually an input stream object, that is, an InputStream object, such as standard input. This is because the output of the program is similar to us typing to standard input, so its goal is an input stream)🎜 🎜This is equivalent to you getting the output stream of program A. The key now is to assign this output stream to the input stream of program B. 🎜 🎜You can convert the output stream of program A (an InputStream object) obtained earlier into an OutputStream object (very simple, read from the former and directly send it to the latter Just write), and then assign this output stream object to the input stream of the Executor object of program B in the same way as program A. In this way, the input and output streams of programs A and B are connected. 🎜 🎜It is recommended to encapsulate this process, for example, it is called Pipe class, which is used to realize the pipeline connection between two processes. 🎜
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template