Trap and write out error to file

Discussion in 'Java' started by John Leslie, Jul 14, 2004.

  1. John Leslie

    John Leslie Guest

    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; }
    John Leslie, Jul 14, 2004
    #1
    1. Advertising

  2. 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
    Fred L. Kleinschmidt, Jul 14, 2004
    #2
    1. Advertising

  3. John Leslie

    news.rcn.com Guest

    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" <> wrote in message
    news:...
    > 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; }
    news.rcn.com, Jul 15, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jack Wright
    Replies:
    0
    Views:
    5,332
    Jack Wright
    Feb 29, 2004
  2. Jack Wright
    Replies:
    0
    Views:
    2,951
    Jack Wright
    Feb 29, 2004
  3. sam
    Replies:
    1
    Views:
    1,624
    bruce barker
    Nov 2, 2004
  4. Peter Hansen
    Replies:
    1
    Views:
    369
    Joona I Palaste
    Aug 24, 2003
  5. Peter Hansen
    Replies:
    1
    Views:
    410
    Joona I Palaste
    Aug 24, 2003
Loading...

Share This Page