I have a UNIX native executable that requires the arguments to be fed in like this
prog.exe < foo.txt.
foo.txt has two lines: bar baz
I am using java.lang.ProcessBuilder to execute this command. Unfortunately, prog.exe will only work using the redirect from a file. Is there some way I can mimic this behavior in Java?
Of course,
ProcessBuilder pb = new ProcessBuilder("prog.exe", "bar", "baz");
does not work.
Thanks!
The redirection is setup by the shell you need todo this manually, something like that:
Not tested, but something like this should work.
I ended up doing something like this and it works. Thanks for all the help!
Did you try to wrap
prog.exe
into a script which accepts arguments and deal withprog.exe
? I assume you're using a shell, so it's quite simple to set up a bash script.If I understand your problem, the script would look like :
Build the process using a
ProcessBuilder
, then useprocess.getOutputStream()
to get an OutputStream that will pipe to the standard input of the process.Open the file using normal Java techniques, read the contents of the file and write it to the
OutputStream
going to the Process you made with theProcessBuilder
.The problem you have right now is that you're calling the
ProcessBuilder
to launchWhich is probably nothing close to what you want to achieve.