简体   繁体   English

将字符串管道输入Java Runtime.exec()作为输入

[英]Piping a string into Java Runtime.exec() as input

I have a string that I need to pipe into an external program, then read the output back. 我有一个字符串,我需要管道到外部程序,然后读回输出。 I understand how to read the output back, but how do I pipe this string as input? 我理解如何读回输出,但如何将此字符串作为输入管道? Thanks! 谢谢!

It goes like this (uncompiled and untested) 它是这样的(未编译和未经测试)

Process p = Runtime . getRuntime ( ) . exec ( ... ) ;
Writer w = new java . io . OutputStreamWriter ( p . getOutputStream ( ) ) ;
w . append ( yourString ) ;
w. flush ( ) ;
// read the input back

Careful that you don't create a deadlock. 小心你不要造成僵局。 Reading/writing to the process in the same thread can be problematic if the process is writing data you are not reading and meanwhile you are writing data it is not reading. 如果进程正在写入您没有读取的数据,同时您正在编写不读取的数据,那么在同一个线程中读取/写入进程可能会有问题。

I tend to use a little pattern line this to get the io going in different threads: 我倾向于使用一个小图案线来让io进入不同的线程:

import java.io.InputStream;
import java.io.OutputStream;

public final class Pipe implements Runnable {

    private final InputStream in;
    private final OutputStream out;

    public Pipe(InputStream in, OutputStream out) {
        this.in = in;
        this.out = out;
    }

    public static void pipe(Process process) {
        pipe(process.getInputStream(), System.out);
        pipe(process.getErrorStream(), System.err);
        pipe(System.in, process.getOutputStream());
    }

    public static void pipe(InputStream in, OutputStream out) {
        final Thread thread = new Thread(new Pipe(in, out));
        thread.setDaemon(true);
        thread.start();
    }

    public void run() {
        try {
            int i = -1;

            byte[] buf = new byte[1024];

            while ((i = in.read(buf)) != -1) {
                out.write(buf, 0, i);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

This may or may not apply to what you want to do -- maybe your dealing with an established set of input that is guaranteed to show up after certain expected output (or vice versa, or several iterations of that). 这可能适用于您想要做的事情,也可能不适用 - 也许您处理的是已建立的输入集合,保证在某些预期输出后显示(反之亦然,或者几次迭代)。 Ie some synchronous block and step kind of interaction. 即一些同步块和步骤类型的交互。

But if you are simply 'watching' for something that might show up then this is a good start in the right direction. 但如果你只是“观察”可能出现的东西,那么这是一个正确方向的良好开端。 You would pass in some different streams and work in some java.util.concurrent 'await(long,TimeUnit)' type code for waiting for the response. 您将传入一些不同的流并使用一些java.util.concurrent'await(long,TimeUnit)'类型代码来等待响应。 Java IO blocks basically forever on read() ops, so separating yourself from those threads will allow you to give up after a certain time if you don't get the expected response from the external process. Java IO基本上永远阻塞在read()操作上,因此如果您没有从外部进程获得预期的响应,那么将您自己与这些线程分开将允许您在一定时间后放弃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM