I using StreamGobbler to execute a command line system call. The class seems to work just fine. My problem is that when I try to catch my errors it always comes back "null". Basically I have 3 text boxes that need to be filled in and then the user hits the execute button to run the command. If I mistype something in the first two text boxes (txt_server and txt_server2) teh error that gets returned is "null". If I mistype something in the 3rd text box (txt_VOBStore) I get a usefull error that pops up...which is what I would expect. Below is the code for my main class and my StreamGobbler class. Please take a look over the code and let me know where I might be going wrong...
String Error = "";
Process p = null;
try {
p = Runtime.getRuntime().exec(command);
// any output?
StreamGobbler outputGobbler = new
StreamGobbler(p.getInputStream(), "OUTPUT");
StreamGobbler errorGobbler = new
StreamGobbler(p.getErrorStream(), "ERROR");
// kick them off
outputGobbler.run();
errorGobbler.run();
Error = errorGobbler.Error;
int returnVal = p.waitFor();
if (returnVal != 0) {
throw ( new Throwable());
}
} catch (Exception e){
JOptionPane.showMessageDialog(null,
"Could not change file protections: " + e.getMessage(), "Error", JOptionPane.WARNING_MESSAGE);
return;
} catch (Throwable T){
if (Error != null){
JOptionPane.showMessageDialog(null,
"Could not change file protections: " + Error, "Error", JOptionPane.WARNING_MESSAGE);
return;
}else
JOptionPane.showMessageDialog(null,
"Could not change file protections: " + T.getMessage(), "Error", JOptionPane.WARNING_MESSAGE);
return;
}
Here is my SG Class...
import java.io.InputStream;
import java.util.*;
import java.io.*;
import javax.swing.JOptionPane;
/*
* streamGobbler.java
*
* Created on August 2, 2007, 1:32 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
/**
*
* @author br
*/
public class StreamGobbler extends Thread {
InputStream is;
String type;
String Error = "";
/** Creates a new instance of streamGobbler */
public StreamGobbler(InputStream is, String type) {
this.is = is;
this.type = type;
}
public void run(){
try
{
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line=null;
while ( (line = br.readLine()) != null)
Error = br.readLine();
// System.out.println(Error);
} catch (IOException ex)
{
JOptionPane.showMessageDialog(null,
"Could not change VOB protections: " + ex.getMessage(), "Error", JOptionPane.WARNING_MESSAGE);
return;
}
}
}