Reading an output Stream

I am running a DOS command within a java program. If the command kicks back an error I want the error to print to the screen. If there is no error then nothing will be printed to the screen. I am using waitFor() to determine if an error is thrown. I am using a reader to read the output stream (I'm guessing that this is how I read the DOS error), but it prints funky stuff and not the error message.

This is what I get when I try to output the errors:

Process exitValue: java.io.BufferedReader@aeffdf

Process exitValue: java.io.BufferedReader@120a47e

Process exitValue: java.io.BufferedWriter@f73c1

Here is the code that I have.

privatevoid btn_RenameActionPerformed(java.awt.event.ActionEvent evt){

String VOBpath = txt_VOBpath.getText();

String CReplica = txt_Currentrepname.getText();

String NReplica = txt_Newrepname.getText();

String command = ("cmd /c multitool rename replica:" + CReplica +" "+ NReplica);

File filepath =new File(VOBpath);

Process p =null;

try{

p = Runtime.getRuntime().exec(command, null, filepath);

int exitVal = p.waitFor();

//System.out.println("Process exitValue: " + exitVal);

if(exitVal == 1){

throw (new Throwable());

}

}catch (IOException ex){

JOptionPane.showMessageDialog(null,

"Cannot Rename Replica: " + ex.getMessage(),"Error", JOptionPane.WARNING_MESSAGE);

return;

}catch (InterruptedException in){

JOptionPane.showMessageDialog(null,

"Cannot Rename Replica: " + in.getMessage(),"Error", JOptionPane.WARNING_MESSAGE);

return;

}catch (Throwable T){

BufferedReader stdInput =new BufferedReader(new

InputStreamReader(p.getInputStream()));

BufferedWriter stdOutput =new BufferedWriter(new

OutputStreamWriter(p.getOutputStream()));

BufferedReader stdError =new BufferedReader(new

InputStreamReader(p.getErrorStream()));

System.out.println("Process exitValue: " + stdInput);

System.out.println("Process exitValue: " + stdError);

System.out.println("Process exitValue: " + stdOutput);

//JOptionPane.showMessageDialog(null,

//"Cannot Rename Replica: " + T.getMessage(), "Error", JOptionPane.WARNING_MESSAGE);

return;

}

}

Where am I going wrong?

Thanks.

[3705 byte] By [yelllow4ua] at [2007-11-27 11:57:00]
# 1

You are printing the objects that hold the data and so you are printing to screen the object reference. try using the readLine() method to the bufferedreaders. e.g. stdError.readLine();

ita6cgra at 2007-7-29 19:10:45 > top of Java-index,Java Essentials,New To Java...
# 2

Also why are you doing ex.getMessage() and in.getMessage() ? you do ex and in no point in adding extra

mark07a at 2007-7-29 19:10:45 > top of Java-index,Java Essentials,New To Java...
# 3

I am getting an IO error and it says that readLine() isn't a method of stdOutput. I see a ToString() method. Would that work.

Here is my broken code so far....

private void btn_RenameActionPerformed(java.awt.event.ActionEvent evt) {

String VOBpath = txt_VOBpath.getText();

String CReplica = txt_Currentrepname.getText();

String NReplica = txt_Newrepname.getText();

String command = ("cmd /c multitool rename replica:" + CReplica + " "+ NReplica);

File filepath = new File(VOBpath);

Process p = null;

try {

p = Runtime.getRuntime().exec(command, null, filepath);

int exitVal = p.waitFor();

//System.out.println("Process exitValue: " + exitVal);

if(exitVal == 1){

throw ( new Throwable());

}

} catch (IOException ex) {

JOptionPane.showMessageDialog(null,

"Cannot Rename Replica: " + ex.getMessage(), "Error", JOptionPane.WARNING_MESSAGE);

return;

} catch (InterruptedException in){

JOptionPane.showMessageDialog(null,

"Cannot Rename Replica: " + in.getMessage(), "Error", JOptionPane.WARNING_MESSAGE);

return;

} catch (Throwable T){

BufferedReader stdInput = new BufferedReader(new

InputStreamReader(p.getInputStream()));

BufferedWriter stdOutput = new BufferedWriter(new

OutputStreamWriter(p.getOutputStream()));

BufferedReader stdError = new BufferedReader(new

InputStreamReader(p.getErrorStream()));

String stdInput2 = stdInput.readLine();

String stdError2 = stdError.readLine();

String stdOutput2 = stdOutput.readLine();

System.out.println("Process exitValue: " + stdInput2);

System.out.println("Process exitValue: " + stdError2);

System.out.println("Process exitValue: " + stdOutput2);

//JOptionPane.showMessageDialog(null,

//"Cannot Rename Replica: " + T.getMessage(), "Error", JOptionPane.WARNING_MESSAGE);

return;

}

}

yelllow4ua at 2007-7-29 19:10:45 > top of Java-index,Java Essentials,New To Java...
# 4

ok... I got it to compile now, but I never get the error. Everything always equals 'null'. For some reason the reader isn't reading the DOS error message that is ouput....

Please help.....lol

private void btn_RenameActionPerformed(java.awt.event.ActionEvent evt) {

String Output = null;

String Error = null;

String Input = null;

String VOBpath = txt_VOBpath.getText();

String CReplica = txt_Currentrepname.getText();

String NReplica = txt_Newrepname.getText();

String command = ("cmd /c multitool rename replica:" + CReplica + " "+ NReplica);

File filepath = new File(VOBpath);

Process p = null;

try {

p = Runtime.getRuntime().exec(command, null, filepath);

int exitVal = p.waitFor();

System.out.println("Process exitValue: " + exitVal);

if(exitVal == 1){

throw ( new Throwable());

}

BufferedReader stdInput = new BufferedReader(new

InputStreamReader(p.getInputStream()));

BufferedWriter stdOutput = new BufferedWriter(new

OutputStreamWriter(p.getOutputStream()));

BufferedReader stdError = new BufferedReader(new

InputStreamReader(p.getErrorStream()));

Input = stdInput.readLine();

Error = stdError.readLine();

Output = stdOutput.toString();

} catch (IOException ex) {

JOptionPane.showMessageDialog(null,

"Cannot Rename Replica: " + ex.getMessage(), "Error", JOptionPane.WARNING_MESSAGE);

return;

} catch (InterruptedException in){

JOptionPane.showMessageDialog(null,

"Cannot Rename Replica: " + in.getMessage(), "Error", JOptionPane.WARNING_MESSAGE);

return;

} catch (Throwable T){

System.out.println("Process exitValue: " + Input);

System.out.println("Process exitValue: " + Error);

System.out.println("Process exitValue: " + Output);

//JOptionPane.showMessageDialog(null,

//"Cannot Rename Replica: " + T.getMessage(), "Error", JOptionPane.WARNING_MESSAGE);

return;

}

}

yelllow4ua at 2007-7-29 19:10:45 > top of Java-index,Java Essentials,New To Java...
# 5

well first don't set your Strings and stuff equal to null... do String Output = ""; works much better and make sure you path for the .exec is correct

mark07a at 2007-7-29 19:10:45 > top of Java-index,Java Essentials,New To Java...
# 6

I changed the strings to be equal to "" and now I get nothing kicked back. If I put perfect information in the text boxes and hit the execute command the command works flawlessly. But if I put something screwy in the text boxes it just stares at me and outputs nothing. If I run the same command from the command line with screwy information it gets a nice error that tells me what went wrong with the command. I just want that same error to be output word for word when the command is run from my java program. It doesn't seem as if these buffers are icking anything up. Is there another way to pick up errors from a DOS command?

Thanks.

yelllow4ua at 2007-7-29 19:10:45 > top of Java-index,Java Essentials,New To Java...
# 7

Initializing your String values to null would have been fine, since you aren't trying to append to them.

Your code doesn't make sense, though. If your external process ends with an error, you throw a Throwable (I don't think creating and throwing a Throwable *ever* makes sense, but that's another story). Throwing that Throwable causes you to jump right over your getInputStream/getOutputStream/getStdErr calls, and your Strings still have their initial value.

If you are getting "1" back from "waitFor", don't throw an Exception-just get the streams and try outputting something from them. I don't know if they are still valid when "waitFor" is finished (they might be valid, but I just don't know). You might have to try reading from them before "waitFor" is called. You can always get the exit value without waitFor (see methods of Process).

And read this, if you haven't already:

When Runtime.exec() won't

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

As a side note, standard Java coding conventions state that the names of your variables should start with a lowercase letter. You should follow this convention.

doremifasollatidoa at 2007-7-29 19:10:45 > top of Java-index,Java Essentials,New To Java...