"Here documents" are implemented by the parser of the shell; the here document is not read as if from the command line, but rather the shell switches from grabbing command lines to simply reading from standard input in unbuffered mode. So sending the data as a command line, as you're doing here, won't work. Two suggestions I can make to you: the first one would be to write the "here document"'s text to a real temporary file (call it 'inputfile'), then use standard redirection, which still requires a little song and dance -- you must say
sh -c "/bin/whatever arg arg arg < inputfile"
The other option is to feed the data into the process's standard input from the Java program, something like
Process p = Runtime.exec(...); OutputStream os = p.getOutputStream(); PrintWriter pw = new PrintWriter(new OutputStreamWriter(os), true); pw.println("password"); pw.println("whatever"); pw.println("exit"); pw.close();
Wonderful, thanks for the information. Unfortunately, I am not a java pro yet. My exec code does the following:
I see it closing OutputStream at the top fo run() and the it writes stuff to StringBuffers via the append() and appendEx() functions. The exec calls start() and join(). I take it start() calls the run() function. Do I put your pw writer stuff before it closes the output stream and then let it close the output stream?
Okay, the printwriter idea worked fine for srvrmgr command. The only stuff I wrote was pw.println(password) and pw.println("exit"); I am not sure why, but if I do the same thing for another program (repimexp), it does not work. However, if I do from the command line the <<EOF ... EOF steps to pass in the password and exit it does work for repimexp. Any ideas on what I can look at on why it is not working for the repimexp?
posted 13 years ago
If it is waiting for the password on stderr instead of stdout (possible?), how do I do something like pw.println() and have it go to stderr instead of stdout?
Nothing? Or something? Like this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop