views:

24

answers:

1

Hi,

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 (ExecuteException 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

A: 

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
...
Andrzej Doyle
Thanks, but unfortunately this is not what I want. I want to get feedback in my cmd prompt when I type something, which I don't. When I run my example I get the "=>" prompt, but when I type something it's not visible. It shows up when I press enter though. Also, execute blocks until the spawned process exits so I don't see how your example would do anything different?!
Raoul Duke