This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
Runtime.getRuntime().exec(commandToExecute) without promting for enter key to be pressed
Sg Ramkumar
Greenhorn
Joined: Jun 21, 2010
Posts: 5
posted
0
Hi,
I'm having a shell script that is to be invoked from a java program.
The shell script is like:
# /tmp/a.sh
#!/bin/sh
read a
echo "this is the next line"
I want the java program to execute this shell script without prompting for "enter key" to be pressed
String commandToExecute = "sh /tmp/a.sh";
Process p = Runtime.getRuntime().exec(commandToExecute);
The above 2 lines are part of my java program. Executing the above java code hangs waiting for input.
Need help on getting the java code lines to automatically accept enter key and go next on the shell script
Open the Process stdin (Process.getOutputStream() ) and write the desired bytes. Depending on the detail of what you are doing you may need to write an end-of-line and then to flush() or close() the stdin stream.
Retired horse trader.
Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
You need to handle the Process stderr stream as recommended in the 'traps' article cited earlier by Seetharaman Venkatasamy. You might get away without handling stderr this time but at some point in this project or the next it will jump up and bite you. If you are just discarding the Process stdout then you can either use ProcessBuilder rather than Runtime.exec() and merge stderr into stdout using ProcessBuilder.redirectErrorStream(true) or you can perform the merge within the shell by appending 2>&1 to your command.
James Sabre wrote:or you can perform the merge within the shell by appending 2>&1 to your command.
Except that the JVM will act as a shell for the newly launched process. Things like piping (using | to send output from one process as input to a next), || and && (conditional execution of a second process) and stream redirecting cannot be handled by passing that to Runtime.exec() or ProcessBuilder. You'll need to implement those yourself.
Sure, you can use redirecting for the JVM itself but I'm not sure that will be propagated to the launched process. I much prefer your redirecting with ProcessBuilder suggestion.
James Sabre wrote:or you can perform the merge within the shell by appending 2>&1 to your command.
Except that the JVM will act as a shell for the newly launched process. Things like piping (using | to send output from one process as input to a next), || and && (conditional execution of a second process) and stream redirecting cannot be handled by passing that to Runtime.exec() or ProcessBuilder. You'll need to implement those yourself.
Sure, you can use redirecting for the JVM itself but I'm not sure that will be propagated to the launched process. I much prefer your redirecting with ProcessBuilder suggestion.
Sorry but I have to disagree. I have just run
which runs the script test.sh using the CWD as $HOME and sends all the stdout from the script test.sh to stderr.
Without the "cd $HOME" the script runs using a CWD of where I execute the java from i.e. "$HOME/work/dev/projects/scratch" and fails since it relies on finding a specfic directory in the CWD. With the "cd $HOME" the script runs as expected. The redirection of stdout to stderr works just as expected.
The important point is that the "-c" says take the next argument as a bash script and this script can contain just about anything that is a valid shell script. The pipe ('|') and background ('&') and conditional operators ('||' and '&&') and redirection all work as one would expect. There may be limitations but I have never come across any and I have used Runtime.exec() with bash and csh for many years.
On Windows using cmd.exe things are very different but the OP is dealing with sh.