开发者

run interactive command with apache commons exec

I want to run an interactive command with apache commons exec. Everything works except that when my command is executed and waits for user input I don't see my input in the console until I press enter which makes it effectively unusable.

This is an example of an interactive program:

public static void main(String[] args) {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String line = null;
        while (true) {
            System.out.print("=> ");
            try {
                line = in.readLine();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println(line);
        }
    }

Now I want to execute that with apache commons exec like this:

public static void main(String[] args) {
    Executor ex = new DefaultExecutor();
    ex.setStreamHandler(new PumpStreamHandler(System.out, System.err, System.in));
    CommandLine cl = new CommandLine("java");
    cl.addArguments("-cp target\\classes foo.bar.Main");

    try {
        ex.execute(cl);
    } catch (ExecuteE开发者_JAVA技巧xception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

As I said, it basically works, I get the "=>" prompt but when I type something I don't see it until I hit enter. I'm doing this on windows 7 with a cmd prompt. I'd appreciate any hint on how to achieve the desired behaviour.

Edit: It works as expected on linux. I guess this is an issue with the windows cmd prompt. I'd still like to make this work if at all possible, so I would appreciate any insight into this behaviour on windows.

Edit2: I also tested with msys shell and powershell, both exhibit the same problem.

Edit3: I worked around the issue by launching a seperate cmd prompt. This works, but I still like to understand why.

CommandLine cl = new CommandLine("cmd");
cl.addArguments("/C java -cp target\\classes foo.bar.Main");

thanks

Raoul


I'm not sure exactly what you were expecting to happen here; if the spawned process is designed to wait to read from its input, then it shouldn't be surprising when it does exactly that?

If that's the issue, and your question is "How can I make my program automatically send a newline character to the spawned process' input?", then you'll need to define an OutputStream to write the input to, and get hold of the ExecuteStreamHandler to attach it to the process. Something like the following:

Executor ex = new DefaultExecutor();

// Create an output stream and set it as the process' input
OutputStream out = new ByteArrayOutputStream();
ex.getStreamHandler().setProcessInputStream(out);
...
try
{
    ex.execute(cl);
    out.write("\n".getBytes()); // TODO use appropriate charset explicitly
...


Using Apache exec org.apache.commons.exec.DefaultExecuteResultHandler you can launch a non-blocking command. And then you can follow the steps @Andrzej mentioned.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜