Runtime.getRuntime().exec()  problem

Posted on 2006-11-06
Last Modified: 2010-03-31
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)
Question by:allelopath
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 86

Assisted Solution

CEHJ earned 150 total points
ID: 17882458

Author Comment

ID: 17882726
I don't see a solution here, some parts I've covered, and some parts are focussed on Windows.
LVL 86

Accepted Solution

CEHJ earned 150 total points
ID: 17882775
The most important part concerns the necessity (nearly always) of using separate threads to read the streams
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

LVL 24

Assisted Solution

sciuriware earned 100 total points
ID: 17883132
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,


Author Comment

ID: 17884550
LVL 86

Expert Comment

ID: 17885198

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question