I am trying to execute a program from the Java code. Here is my code:
public static void main(String argv[]) {
try {
String line;
Process p =
The problem is happening because standard output of the process is being directed to the file, so the java Process
cannot collect the output.
If you want the output written to a file, and be available for reading in java, then you have two options:
new FileReader("OutputFile.txt")
. Note that exec()
is asynchronous, and can return before the process has finished writing to file, so you will need to wait for the Process to exit, by using Process.waitFor()
. FileWriter
.The bottom line is that you can't use both redirect and read the output from the Process at the same time - it's one or the other.
EDIT: Your comments indicate that 1) is the preferred approach. This will write the output to a file (using the OS shell) which you then read in from java when the process exits.