Results 1 to 7 of 7
Thread: Question on Exceptions
- 07-25-2007, 04:37 PM #1
Member
- Join Date
- Jul 2007
- Posts
- 7
- Rep Power
- 0
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...
- 07-25-2007, 07:34 PM #2
Member
- Join Date
- Jul 2007
- Posts
- 55
- Rep Power
- 0
The native command (such as xcopy) should return a value once executed. You can check that value to determine if an error occurred.
- 07-25-2007, 08:31 PM #3
Member
- Join Date
- Jul 2007
- Posts
- 7
- Rep Power
- 0
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;
}
- 07-25-2007, 08:49 PM #4
Member
- Join Date
- Jul 2007
- Posts
- 55
- Rep Power
- 0
The Process object has an exitValue() method. Try that.
exitValue()
Returns the exit value for the subprocess.
- 07-26-2007, 04:03 PM #5
Member
- Join Date
- Jul 2007
- Posts
- 7
- Rep Power
- 0
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 } }
- 07-26-2007, 06:07 PM #6
Member
- Join Date
- Jul 2007
- Posts
- 7
- Rep Power
- 0
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; } }
- 07-27-2007, 01:41 PM #7
Member
- Join Date
- Jul 2007
- Posts
- 7
- Rep Power
- 0
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
-
How to use chained exceptions
By Java Tip in forum java.langReplies: 0Last Post: 04-04-2008, 02:50 PM -
question regarding exceptions..
By SCS17 in forum New To JavaReplies: 3Last Post: 11-17-2007, 09:31 AM -
Runtime Exceptions
By Java Tip in forum Java TipReplies: 0Last Post: 11-12-2007, 10:31 AM -
how to handle exceptions
By paty in forum Advanced JavaReplies: 2Last Post: 08-05-2007, 04:17 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks