Runtime.getRuntime().exec() problem

I am having difficulty getting Runtime.getRuntime().exec() to work.
If I run the java program (which calls an external fortran program), all goes well in the sense that there are no errors, but nothing seems to happen.  If I debug and stop execution after the exec() line, p.hasExited = true and p.exitcode = 127.  The fortran proglram, when executed from a command line, outputs a few lines of text and also writes 2 output files. I don't see either of these happening when attemting to execute from the java program. The line "System.out.println(line);" is never accessed.  I have checked and double-checked my command line for accuracy.  Code below.  Running on linux, fwtw.

public void runProgram (String cwd, String commandLine)
  // current working directory (where external program resides)
  File filecwd = new File(cwd);
  String[] environmentVariables = {""};

     String line;
     Process p = Runtime.getRuntime().exec(commandLine,
                                                                               environmentVariables, // empty
     BufferedReader input = new BufferedReader (new InputStreamReader(p.getInputStream()));  
     // BufferedWriter output = new BufferedWriter(new OutputStreamWriter(p.getOutputStream())); // ?
     while ((line = input.readLine()) != null)
    catch (Exception err)
Who is Participating?

Improve company productivity with a Business Account.Sign Up

CEHJConnect With a Mentor Commented:
The most important part concerns the necessity (nearly always) of using separate threads to read the streams
allelopathAuthor Commented:
I don't see a solution here, some parts I've covered, and some parts are focussed on Windows.
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

sciuriwareConnect With a Mentor Commented:
As a first improvement add after:

     BufferedReader input = new BufferedReader (new InputStreamReader(p.getInputStream()));  

the next statement:


The next improvement is to create 1 or 2 threads (input from the process or in/output both),
that write what you want to input by the process and collect what it wrote to you.

      Xq outputThread = new Xq(p.getInputStream());  // A class that collects the incoming input in itself.

       p.waitFor();                                                      // Wait for the process to end
       outputThread.join();                                          // Wait for the reader thread to get all data (till EOF)

       String result = outputThread.getOutput();            // A 'getter' in the Xq class to release its collected data, String in this example.

This should work fine,

allelopathAuthor Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.