Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Trap and write out error to file

Reply
Thread Tools

Trap and write out error to file

 
 
John Leslie
Guest
Posts: n/a
 
      07-14-2004
I am using the following borrowed code to run unix commands.
Occasionally it returns 3, apparently without actually running the
command and I do not know why.

I would like to get more info on the error and write it to a file.

As I am not a java programmer can someone help me amend the code to
write the error details of the t object to a file?

Thanks in advance.



Runtime rt = Runtime.getRuntime();
Process p = rt.exec(Command);
try {
rc = p.waitFor();
} catch (InterruptedException intexc) { rc = 2; }

rt.gc();
} catch (Throwable t) { rc = 3; }
 
Reply With Quote
 
 
 
 
Fred L. Kleinschmidt
Guest
Posts: n/a
 
      07-14-2004


John Leslie wrote:
>
> I am using the following borrowed code to run unix commands.
> Occasionally it returns 3, apparently without actually running the
> command and I do not know why.
>
> I would like to get more info on the error and write it to a file.
>
> As I am not a java programmer can someone help me amend the code to
> write the error details of the t object to a file?
>
> Thanks in advance.
>
> Runtime rt = Runtime.getRuntime();
> Process p = rt.exec(Command);
> try {
> rc = p.waitFor();
> } catch (InterruptedException intexc) { rc = 2; }
>
> rt.gc();
> } catch (Throwable t) { rc = 3; }


This second "catch" clause is illegal - it has no associated "try" (it
would be OK if the rt.gc() call were removed, since it would then be
associated with the first "try".

You should not use rc both as the return for p.waitFor() and as a flag
you set if there is an error. What if p.waitFor returns 2 or 3? How
would your code distinguish whether an error had occurred?

You can print the exception message to stderr easily:
System.err.println( t.getMessage() );
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Common User Interface Services
M/S 2R-94 (206)544-5225
 
Reply With Quote
 
 
 
 
news.rcn.com
Guest
Posts: n/a
 
      07-15-2004
Maybe you need to find out if the external command is running or not. Try
rt.exec ( Command + " > /tmp/myRuntimeOutput.out 2>&1" )
to capture stdout and stderr into a file.

If the exec() call returns '3', which you get back from waitFor(), your Unix
command probably failed. This could look like it didn't execute because the
error messages will sail off into the ether if you don't grab it and
redirect it into a file.

Hope this helps.

jim



"John Leslie" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> I am using the following borrowed code to run unix commands.
> Occasionally it returns 3, apparently without actually running the
> command and I do not know why.
>
> I would like to get more info on the error and write it to a file.
>
> As I am not a java programmer can someone help me amend the code to
> write the error details of the t object to a file?
>
> Thanks in advance.
>
>
>
> Runtime rt = Runtime.getRuntime();
> Process p = rt.exec(Command);
> try {
> rc = p.waitFor();
> } catch (InterruptedException intexc) { rc = 2; }
>
> rt.gc();
> } catch (Throwable t) { rc = 3; }



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
maxRequestLength and Trap Page Not Found error sam ASP .Net 1 11-02-2004 05:33 PM
How to trap System.Web.HttpException: Request timed out.? Jack Wright ASP .Net 0 02-29-2004 12:13 PM
How to trap System.Web.HttpException: Request timed out.? Jack Wright ASP .Net 0 02-29-2004 12:13 PM
How to open a new console and write something out write printf() Peter Hansen C++ 1 08-24-2003 11:49 AM
How to open a new console and write something out write printf() Peter Hansen C Programming 1 08-24-2003 11:49 AM



Advertisments