ryno71
asked on
Questions on Runtime.getRuntime()
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
It's difficult to say whether it would work. Try it out
ASKER
Would I run into issues if I don't pay attention to the ErrorStream and only worry about the InputStream?
Again it's difficult to say
ASKER
thanks nothing abnormal, but might see something if I am playing with large amounts of data.
Have a nice weekend
ryno71
Have a nice weekend
ryno71
:-)
ASKER