Results 1 to 2 of 2
  1. #1
    coolFrenzi is offline Member
    Join Date
    Nov 2008
    Posts
    11
    Rep Power
    0

    Default [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();
    }
    }

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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

  1. runtime.exec stale process
    By karine in forum Advanced Java
    Replies: 6
    Last Post: 10-06-2008, 05:40 PM
  2. Tomcat and Runtime.exec
    By karine in forum Introductions
    Replies: 1
    Last Post: 10-03-2008, 08:25 AM
  3. Runtime.exec()
    By hknyo in forum Advanced Java
    Replies: 2
    Last Post: 08-16-2008, 12:40 AM
  4. Problem with Runtime.exec()
    By nhabibi in forum Advanced Java
    Replies: 11
    Last Post: 07-02-2008, 01:35 AM
  5. Runtime.exec(), handling input and output streams
    By crookshank in forum New To Java
    Replies: 0
    Last Post: 06-05-2008, 02:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •