problems with getOutputStream of Runtime.exec

Hi, I have a problem with the method getOutputStream() of the Process generated by Runtime.exec

I need java to execute the command

Bioinfo/ncoils/ncoils -f < template.fasta

My code is:

Process p = Runtime.getRuntime().exec("Bioinfo/ncoils/ncoils -f");

BufferedReader in = new BufferedReader(new FileReader("template.fasta"));

String line;

OutputStream os = p.getOutputStream();

PrintWriter w1 = new PrintWriter(os);

while( (line=in.readLine()) != null) w1.println(line);

in.close();

os.close();

w1.close();

p.waitFor()

But the program ncoils actsas if it has not received any input .....

Could you help me find what's wrong?

Thanks a lot! Francesco

[755 byte] By [magodellepercussionia] at [2007-10-3 4:57:57]
# 1
What is "template.fasta"?
bschauwejavaa at 2007-7-14 23:03:20 > top of Java-index,Java Essentials,Java Programming...
# 2
It's a text file
magodellepercussionia at 2007-7-14 23:03:20 > top of Java-index,Java Essentials,Java Programming...
# 3

> os.close();

I don't know if this might have something to do with it, but you're closing the unbuffered stream before you close the buffered one that wraps it (w1). You should just close w1 here, not both.

Closing w1 will flush its output (to os), and then close os, in the right order.

warnerjaa at 2007-7-14 23:03:20 > top of Java-index,Java Essentials,Java Programming...
# 4
I've done the change, but it is still not working ....
magodellepercussionia at 2007-7-14 23:03:20 > top of Java-index,Java Essentials,Java Programming...
# 5
Anyway, what is the EXACT java set of commands which substitutes the linux shell commandprogramName < inputFileName?I'm able to manage the output redirection ( > ) but I'm not able to get the input redirection work ....
magodellepercussionia at 2007-7-14 23:03:20 > top of Java-index,Java Essentials,Java Programming...
# 6

There are many pitfalls to Runtime.exec, mostly centered around the fact that the thing you execute might be writing stuff to stdout and/or stderr and requires something to consume those streams, otherwise it blocks until the output buffer(s) are consumed. Perhaps you are running into some of those pitfalls. Please read and consider this article:

http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

warnerjaa at 2007-7-14 23:03:20 > top of Java-index,Java Essentials,Java Programming...