Results 1 to 2 of 2
- 11-14-2008, 06:41 AM #1
Member
- Join Date
- Nov 2008
- Posts
- 11
- Rep Power
- 0
[SOLVED] Issue with Using Runtime.exec to run an external program
I am running a jar file from within my java program using Runtime.exec . The jar file is an application that reads two input files and writes output to an output file "out.txt". My program then reads this output file.
After running my program for the first time, I get the followingg error: java.io.FileNotFoundException: out.txt
But on running the program for the second time, I get the desired output, ie, the program reads the file.
It seems that the output file is created after the program attempts to read it in the first try. In the second and consecutive try, the file is already created so it reads it.
So how do I make my program read the newly created file in the first attempt?
Code:
import java.io.*;
public class test1
{
BufferedReader in;
String read;
public test1() throws InterruptedException
{
try
{
Runtime.getRuntime().exec("java -jar XYZ.jar -q " + "in1.txt" + " -d " + "in2.txt");
}
catch (Exception e)
{
e.printStackTrace();
}
readScore();
}
void readScore()
{
try
{
in = new BufferedReader(new FileReader("out.txt"));
read = in.readLine();
System.out.println("test output: " + read);//print out the line
in.close();
}
catch(IOException e)
{
System.out.println("There was a problem:" + e);
}
return;
}
public static void main(String[] args) throws InterruptedException
{
test1 test = new test1();
}
}
- 11-14-2008, 06:56 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
You're not waiting for the process to finish.
See When Runtime.exec() won't - JavaWorld
Read that completely and thoroughly and you'll save yourself a lot of headaches with future problems.
Similar Threads
-
runtime.exec stale process
By karine in forum Advanced JavaReplies: 6Last Post: 10-06-2008, 05:40 PM -
Tomcat and Runtime.exec
By karine in forum IntroductionsReplies: 1Last Post: 10-03-2008, 08:25 AM -
Runtime.exec()
By hknyo in forum Advanced JavaReplies: 2Last Post: 08-16-2008, 12:40 AM -
Problem with Runtime.exec()
By nhabibi in forum Advanced JavaReplies: 11Last Post: 07-02-2008, 01:35 AM -
Runtime.exec(), handling input and output streams
By crookshank in forum New To JavaReplies: 0Last Post: 06-05-2008, 02:41 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks