4

I am new to java. I am tasked to write java program to run the command lines. I tested the command line under the DOS prompt since i do not have have access to Linux box yet. it worked fine. See the PROGRAM below for full command line syntax. the job will take 6 input files and generate some output files. Next i tried to create a class to and using getruntime and process to process this job. Even it compiled without error but when i run it just show the cursor blinking... i thought i need to use Thread async technique. please provide some advices since i do not have enough time for the projects. I also would like to implement a call back or return values when the job is done. an example would be greatly appreciated. Thanks

import java.io.*;
 public class RunJob {

 // public static final String PROGRAM = "c:\\wrk\\java.exe Hello";

//one command line below
 public static final String PROGRAM = "c:/java.exe -cp \"wrk/jmp.jar;wrk/colt.jar\"   gov.lanl.yadas.reliability.UltimateMissileReliabilityModel 10000 \"wrk/\" x1.dat x2c.dat x3.dat   x4.dat x5.dat x6.dat true";

 //  Set to true to end the loop 
 static boolean done = false;

public static void main(String argv[]) throws IOException {

    BufferedReader is;   
    String line;
    String returnMsg = "Start ";
    final Process p = Runtime.getRuntime().exec(PROGRAM);
     System.out.println("start");


    Thread waiter = new Thread() {
      public void run() {
        try {
          p.waitFor();
        } catch (InterruptedException ex) {
        System.out.println("InterruptedException");
          return;
        }
        System.out.println("Program terminated!");
        done = true;
      }
    };
    waiter.start();
   is = new BufferedReader(new InputStreamReader(p.getInputStream()));

    while (!done && ((line = is.readLine()) != null))
      {
      System.out.println(line);
      returnMsg = returnMsg + line;      
      } 
      System.out.println(returnMsg);
        System.out.println("End");

     return;
  }// main


}

1 Answer 1

3

I assume that there is a good reason why you want to run a java program from another java program and not just from a shell script, or by invoking an API - but if not - please reconsider.

As to your problem - if your application produces a lot of output (the one you are running as a process) - your application will hang. The p.waitFor() will halt until the process ends. But if you don't read the information from the InputStream - it will overflow and hang!

Advice #1: put the p.waitFor() at the end.
Advice #2: read this article. If I remember correctly it is the one I read when I had a similar problem. You can also google for "StreamGobbler" - it is a common name for a separate thread that "gobbles" your streams.
Advice #3: Don't forget the ErrorStream - if your application will produce too many errors - that stream will cause the process to hang as well.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.