Results 1 to 3 of 3
  1. #1
    yelllow4u is offline Member
    Join Date
    Jul 2007
    Posts
    10
    Rep Power
    0

    Default Trying to catch thread errors

    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...

    Java Code:
         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...

    Java Code:
    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; 
                } 
          }
    }

  2. #2
    henry_78 is offline Member
    Join Date
    Aug 2007
    Posts
    47
    Rep Power
    0

    Default

    Hi,
    When u create a new process if u want to know the errors u have to get a reference to the errorStream of that process(which is not your standard input and standard output). Try this code:

    InpueStream err = p.getErrorStream();
    BufferedReader br = new BufferedReader(new InputStreamReader(err));
    try {
    String line = br.readline();
    while(line != null) {
    System.out.println(line);
    line = br.readline();
    }
    }
    catch(IOException e) {
    }

  3. #3
    yelllow4u is offline Member
    Join Date
    Jul 2007
    Posts
    10
    Rep Power
    0

    Smile Ha

    That makes sense. It also works now. Sometimes I write so much junk I lose track of what is actually happening. I appreciate the help.

Similar Threads

  1. Replies: 0
    Last Post: 01-28-2008, 08:02 AM
  2. Try Catch
    By Renegade85 in forum New To Java
    Replies: 4
    Last Post: 12-03-2007, 05:10 PM
  3. when to use try...catch
    By javaplus in forum New To Java
    Replies: 2
    Last Post: 11-18-2007, 09:52 PM
  4. Use try and catch
    By zoe in forum New To Java
    Replies: 2
    Last Post: 07-25-2007, 08:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •