开发者

Java program stdout and detaching from foreground

I have a java program, let's say Test.class.

When I execute java Test the program ask for a Password and then continute.

The problem is that the stdout is redirected to a log and the program is launched with the & ( we are on UNIX).

How can i interact with this program launched java Test & with the stdin and stdout?

One possible solution i开发者_如何学JAVAs to start the program in foreground and then after a condition run it in background from java.

Thanks!


If the program can read the password from stdin, you can have a Unix script prompt for the password, then start the Java application and pass the password to it, e.g.:

echo $PASSWORD | java Test >log.out &

Or you can consider to split your Java application in two parts; there could be one interactive "front-end" part that validates the password, and then once the password is validated this could launch a "back-end" part as a background process and exit.


One option is to pipe the input to your program using echos as:

(echo input1
 echo input2
 ....
) | java Test >& logfile &

Alternatively if the number of inputs are large you can also put your inputs in a file and redirect the file contents as:

< input_file java Test >& logfile &


I don't see anything Java specific in this question, if you want to drive the stdin based on the application output, you can use the Expect utility: http://en.wikipedia.org/wiki/Expect

Beware though, Expect is notoriously fragile, you'd do wise to refrain from using it in production scenarios.


Actually if you only want to be able to enter the password, perhaps you can try launching your app in foreground (without the trailing &).

Then, after you have entered the password, press Ctrl+Z or in another shell do kill -SIGSTP <pid> in order to suspend your program. Finally, type bg to put it in background.

Read the docs about your shell's job-control functionality for details.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜