Solved

Questions on Runtime.getRuntime()

Posted on 2006-06-30
7
212 Views
Last Modified: 2010-03-31
Guys couple of quick questions on Runtime.getRuntime()

First question ... waitFor?  Is it onyl used for cleanup?  If not for what?
exitCode = p.waitFor();  abnormal termations?


Second question if I want to obtain the output from running an application this way
I know I get via  InputStreamReader and BufferedReader if I was to loop
through the BufferedReader response

while ( (line = br.readLine()) != null)
{
             response=response+line;
}

it would give me all output from the program correct?  (Assuming no abnormal terminations


public static boolean executeObject(byte[] bs)
      {
            boolean resultcode = false;
            String programdir="";
            String programName="";
            String response="";

            
            
            String[] command = new String[2];
            
            
            
            try
            {
                  programdir="D:/ryno71;
                  programName="ryno71.exe";
            } catch (Exception e1)
            {
                  // TODO Auto-generated catch block
                  e1.printStackTrace();
            }
            
            command[0] = programdir+programName;
            String byte_to_string= new String(bs, 0, bs.length);

            command[1] = byte_to_string;  //put what I am executing with ryno71.exe
            //cmd[2] = "param2";
            
            InputStreamReader isr=null;
            BufferedReader br = null;
            try
            {
                  Runtime program = Runtime.getRuntime();
                  Process p = program.exec(command);
                  InputStream stdin = p.getInputStream();
                  
                  
                  
                  
                  try
                  {
                        
                        isr = new InputStreamReader(stdin);
                  }
                  
                  finally
                  {
                        try
                        {
                              if (isr != null) isr.close();
                        }
                        catch(IOException ie)
                        {
                              
                              ie.printStackTrace();
                              
                        };
                        
                        
                  }
                  
                  try
                  {
                        
                        br = new BufferedReader(isr);
                  }
                  
                  finally
                  {
                        
                        try
                        {
                              if (br != null) br.close();
                        }
                        catch(IOException e)
                        {
                              e.printStackTrace();
                              
                        };
                        
                  }
                  
                  String line=null;
                  while ( (line = br.readLine()) != null)
                  {
                                                  response=response+line;
                                                }
                                                //System.out.println(line);
                  
                  //write abnormal out somewhere here.
                  
                  
                  int exitCode = 0;
                  try
                  {
                  exitCode = p.waitFor();  
                                                /*exitCode <= used for errors if you dont want to
                                                     use a separate thread for Process.getErrorStream ?
                                                    Whats waitFor used for????
                                                 */

                  } catch (InterruptedException e)
                  {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                  }
                  if (exitCode != 0)
                  {
                        resultcode=false;
//                        
                        e1.printStackTrace();
                  }
                                                else
                                                {
                                                          resultcode=true;
                                                }

                  
            } catch (IOException e)
            {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            }
            
            
            return resultcode;
      }

As always thanks for the input
0
Comment
Question by:ryno71
[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
  • 4
  • 3
7 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 125 total points
ID: 17018328
>>exitCode = p.waitFor();  abnormal termations?

Normal *and* abnormal

>>it would give me all output from the program correct?

You need to treat the streams on separate threads. See

http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
0
 

Author Comment

by:ryno71
ID: 17018483
Thanks for the quick response.  If I am limited in opening additional new threads... (not allowed to open additional threads) what I have would work for normal output, correct?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17018498
It's difficult to say whether it would work. Try it out
0
Technology Partners: 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!

 

Author Comment

by:ryno71
ID: 17018507
Would I run into issues if I don't pay attention to the ErrorStream and only worry about the InputStream?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17018518
Again it's difficult to say
0
 

Author Comment

by:ryno71
ID: 17019824
thanks nothing abnormal, but might see something if I am playing with large amounts of data.

Have a nice weekend

ryno71
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17019837
:-)
0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb example issues 3 86
how to debug htl and js pages 8 76
Tagging and Merging on Branch 1 66
Java regex 5 18
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

738 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