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

    Angry Question on Exceptions

    OK, I have a quick question on exceptions.

    This basically has to do with system calls. Let's say I run a command from the command line (like DOS on XP). This can be any command it doesn't matter. For an example let's say I open up the command line and type 'xcopy'. Without any parameters I get an error that says 'Invalid number of parameters'. Now lets take this a step further and put the command inside a Java application and make it a system call. How do I know what kind of exception this is? Would this be an application exception since it is coming from the xcopy program from within the command prompt? Is there something that I can put in my code that tells me exactly what kind of exception is going on?

    I have some code I can post if everyone needs a solid example. I am probably making this harder than it needs to be...

  2. #2
    Seemster is offline Member
    Join Date
    Jul 2007
    Posts
    55
    Rep Power
    0

    Default

    The native command (such as xcopy) should return a value once executed. You can check that value to determine if an error occurred.

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

    Default

    Ok...next dumb question....how do I check a value that xcopy kicks back to see if its and error or not. Now I have a swing GUI with some text boxes and an execute button and if I leave the text boxes blank and click the execute button I can get an IO exception to pop up, but I'm not sure how to get errors that come from xcopy itself to pop up.



    Here is some of the code I have...

    try {
    p = Runtime.getRuntime().exec(command, null, filepath);
    } catch (IOException ex) {
    JOptionPane.showMessageDialog(null,
    "Cannot Copy: " + ex.getMessage(), "Error", JOptionPane.WARNING_MESSAGE);
    return;
    }

  4. #4
    Seemster is offline Member
    Join Date
    Jul 2007
    Posts
    55
    Rep Power
    0

    Default

    The Process object has an exitValue() method. Try that.

    exitValue()
    Returns the exit value for the subprocess.

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

    Default I'm getting closer

    OK....I been doing some reading on the exitValue() method and it looks like it throws an IllegalThreadStateException. Is it possible to print the exitValue() value so that I can write code to handle any number thats not zero? I tried to print the value in the code below, but the IllegalThreadStateException is thrown and my value is never printed.

    Here is the code I have:

    Java Code:
      private void btn_RenameActionPerformed(java.awt.event.ActionEvent evt) {                                           
    // TODO add your handling code here:
         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.exitValue();
               System.out.println("Process exitValue: " + exitVal);
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null,
                  "Cannot Rename Replica: " + ex.getMessage(), "Error", JOptionPane.WARNING_MESSAGE);
                return;
            } catch (IllegalThreadStateException I) {
                    return;
            }
        }

    Can I use 'if' statements inside my catch (IllegalThreadStateException I) to deal with the various exitValue's?

    Something like....

    Java Code:
    catch (IllegalThreadStateException I) {
        if (exitVal = 1){
            // Do something
          }
        if (exitVal = 2){
           // Do something
         }
      }

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

    Default

    Update....OK, I am on to something. I have some working code, but what would I need to change to print out the error that is printed on the DOS screen? I determined that when the exitValue is 1 and error has occured....so I throw and exception and cath it. But when I print the error to a message box it just says "null". How do I get the error that is printed in the DOS screen to apear word for word in a message box? Below is what I have so far;-)

    Java Code:
        private void btn_RenameActionPerformed(java.awt.event.ActionEvent evt) {                                           
    // TODO add your handling code here:
         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){
                JOptionPane.showMessageDialog(null,
                  "Cannot Rename Replica: " + T.getMessage(), "Error", JOptionPane.WARNING_MESSAGE);
                return;
            }
        }

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

    Default Buffers maybe???

    So I was thinking maybe I need to use one of the readers and read the output stream? If I run a command from a DOS prompt and it kicks back a response (let's say an error) that response can be read using getOutputStream correct? Or do I need to use getInputStream?

    I tried something like below and all it does is print junk and not what I would expect. I just want the DOS error to be printed if it is detected. :eek:

    Java Code:
     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()));
                 
                 BufferedReader stdOutput = new BufferedReader(new 
                     OutputStreamReader(p.getOutputStream()));
     
                BufferedReader stdError = new BufferedReader(new 
                     InputStreamReader(p.getErrorStream()));
    
               System.out.println("Process exitValue: " + stdInput);
               System.out.println("Process exitValue: " + stdError);
               
                //JOptionPane.showMessageDialog(null,
                  //"Cannot Rename Replica: " + T.getMessage(), "Error", JOptionPane.WARNING_MESSAGE);
                return;
            }
        }

Similar Threads

  1. How to use chained exceptions
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-04-2008, 02:50 PM
  2. question regarding exceptions..
    By SCS17 in forum New To Java
    Replies: 3
    Last Post: 11-17-2007, 09:31 AM
  3. Runtime Exceptions
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-12-2007, 10:31 AM
  4. how to handle exceptions
    By paty in forum Advanced Java
    Replies: 2
    Last Post: 08-05-2007, 04:17 AM

Posting Permissions

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