views:

64

answers:

4

Hi,

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 is to start the program in foreground and then after a condition run it in background from java.

Thanks!

A: 

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 &
codaddict
The problem is that in this way I cannot manage problem with the input. I mean Test class validate the password and if the pwd is invalid it asks again for the password. So only once the password si validate the process can go in background...
Kerby82
+1  A: 

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.

Grodriguez
In this way I cannot manage as I said problem with the input and I cannot restart the ask for password
Kerby82
You may want to split your application in two parts. See my update.
Grodriguez
Can you post a url doc or an example on how to lanch a background process on java? tha back-end is also in java and has to access some variable from the front-end (passing as argument from command line is not allowed).
Kerby82
I was thinking in spawning another `java` process using `nohup`, but this approach would still require you to pass the arguments in the command line. If this is not allowed, then you would need to look for a different solution.
Grodriguez
This is a quite good temporary solution, but also dangerous if the computer is to be secure - other users can find your password just by running command: ps ax
iirekm
A: 

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.

ddimitrov
Using expect means that in some part of the system the password is written, i just want to have a human input
Kerby82
A: 

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.

ddimitrov
I know this way, but it's not so good for the user experience. I was looking some solution to put inside the code itself. However thanks for the suggestion!
Kerby82
Well, given that the process knows its own pid, it can exec a shell with command line that will send SIGSTP to itself and put it in background.
ddimitrov