ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "KUMAR,PANKAJ (HP-Cupertino,ex1)" <pankaj_ku...@hp.com>
Subject How to execute an interactive non-java program from an ant script ?
Date Mon, 13 May 2002 23:07:20 GMT
Hi,

I have a need to launch a text mode interactive non-java application that
prompts user for text input . I looked at Exec task ( in Ant1.5Beta1 ) and
found that this doesn't take care of passing user input to the created
process. To address this I wrote a new task named ExecInteractive  by
deriving it from ExecTask and supplying my own MyPumpStreamHandler class
(implementing from ExecStreamHandler ). This handler creates three separate
threads that write the normal output of the new process to System.out, error
output to System.err and input from System.in to the OutputStream associated
with the process. The code run by each thread is in class MyStreamPumper
modeled after StreamPumper class ( all these are in packacge
org.apache.ant.taskdefs ).

This works okay but with following problems:

1. Whenever the external program prompts the user for input and terminates
the prompt string without a newline then nothing gets written on the
terminal. I changed my code in MyStreamPumper so that it would call
System.out.flush() after writing each byte. However, this causes each
character to be written on a separate line !! I beleive Ant is processing
output through System.out and takes System.out.flush() as indication of
record termination.

I have an ugly workaround for the time being -- I wrote a class AntWrapper
that grabs the System.out and System.in in static variable and then invokes
the main() of Ant. I use these static variables in my MyStreamPumper. To
invoke ant, I call "java <package>.AntWrapper ...".

Would prefer a better solution.

2. The second problem is more insidious. As I mentioned earlier,
MyPumpStreamHandler creates three threads: one for monitotring stdout of the
external program, one for stderr of the external program and one for
keyborad input ( by reading System.in ). When the external program
terminates then the thread monitoring the stdout and stderr of the process
terminate but not the thread that is waiting for input on System.in. The
thread executing ExecStreamHandler is able to do a Thread.join() with these
two threads but is not able to interrupt the thread waiting for keyboard
input.

My workaround is to ask the user ( from thread running the
MyPumpStreamHandler ) to hit ENTER. When the waiting thread reads this and
tries to write to the stdin of the terminated process, it gets exception and
terminates itself.

But I don't like this as it forces the user to do something unnecessary.

Any help is most appreciated.

Pankaj Kumar,
HP Middleware.

--
To unsubscribe, e-mail:   <mailto:ant-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:ant-user-help@jakarta.apache.org>


Mime
View raw message