|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

07-25-2007, 06:37 PM
|
|
Member
|
|
Join Date: Jul 2007
Posts: 7
|
|
|
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, 09:34 PM
|
|
Member
|
|
Join Date: Jul 2007
Posts: 55
|
|
|
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, 10:31 PM
|
|
Member
|
|
Join Date: Jul 2007
Posts: 7
|
|
|
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, 10:49 PM
|
|
Member
|
|
Join Date: Jul 2007
Posts: 55
|
|
|
The Process object has an exitValue() method. Try that.
exitValue()
Returns the exit value for the subprocess.
|
|

07-26-2007, 06:03 PM
|
|
Member
|
|
Join Date: Jul 2007
Posts: 7
|
|
|
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:
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....
catch (IllegalThreadStateException I) {
if (exitVal = 1){
// Do something
}
if (exitVal = 2){
// Do something
}
}
|
|

07-26-2007, 08:07 PM
|
|
Member
|
|
Join Date: Jul 2007
Posts: 7
|
|
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;-)
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, 03:41 PM
|
|
Member
|
|
Join Date: Jul 2007
Posts: 7
|
|
|
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.
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;
}
}
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|