Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Runtime.getRuntime().exec()  problem

Posted on 2006-11-06
6
Medium Priority
?
301 Views
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 = {""};

   try
   {
     String line;
     Process p = Runtime.getRuntime().exec(commandLine,
                                                                               environmentVariables, // empty
                                                                               filecwd);
          
     BufferedReader input = new BufferedReader (new InputStreamReader(p.getInputStream()));  
     // BufferedWriter output = new BufferedWriter(new OutputStreamWriter(p.getOutputStream())); // ?
          
     while ((line = input.readLine()) != null)
     {
        System.out.println(line);
     }
     input.close();
     }
    catch (Exception err)
    {
       err.printStackTrace();
     }
}
0
Comment
Question by:allelopath
[X]
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
6 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 600 total points
ID: 17882458
0
 
LVL 1

Author Comment

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

Accepted Solution

by:
CEHJ earned 600 total points
ID: 17882775
The most important part concerns the necessity (nearly always) of using separate threads to read the streams
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 24

Assisted Solution

by:sciuriware
sciuriware earned 400 total points
ID: 17883132
As a first improvement add after:

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

the next statement:

         p.waitFor();

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.
      outputThread.start();

       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,

;JOOP!
0
 
LVL 1

Author Comment

by:allelopath
ID: 17884550
thanks
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17885198
:-)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

688 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