0

I'm attempting to emulate the functionality of the bash shell script below using a Process or ProcessBuilder object in Java. It's not 100% clear to me how I do the redirection for standard-in. How can I accomplish this?

#
#  Redirect shell echo command from standard output to file
#  This will construct the input file
# 
exec 1> $STDIN
echo -e "$NFILE\n$GFILE\n$INPUT\n$OUTPUT\n$CAX"
exec 1>&-

#
#  Run executable redirecting standard in as the input file and output to a log file
#
"$EXE" < $STDIN >& $LOG
2
  • why exec 1> $STDIN ? Why not just echo -e "..." > $STDIN Commented Sep 22, 2009 at 15:51
  • @mobrule Not sure. I inherited the script. What does exec 1 do differently? Commented Sep 23, 2009 at 15:17

1 Answer 1

2

The Process object streams are misleadingly named. You need to write to the OutputStream of the Process object.

From the doc:

All its standard io (i.e. stdin, stdout, stderr) operations will be redirected to the parent process through three streams (Process.getOutputStream(), Process.getInputStream(), Process.getErrorStream()).

You also have to consume the process output and standard error concurrently, otherwise buffers will fill up and the process will block. See here for more information.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.