Re: Why does this only work when I am running a shellscript

Discussion in 'Java' started by Steven Simpson, Mar 30, 2013.

  1. On 30/03/13 08:46, Cecil Westerhof wrote:
    > I have the following code:
    > private static void doCommand(final String cmd) throws IOException {
    > Process p;
    > Scanner sc;
    >
    > System.out.println("#" + cmd + "#");
    > p = Runtime.getRuntime().exec(cmd);
    > sc = new Scanner(p.getInputStream());
    > while (sc.hasNext()) {
    > System.out.println(sc.nextLine());
    > }
    > }


    You should also drain Process.getErrorStream(). You could discard it,
    copy it to System.err, log it, buffer it to report only if the command's
    exit code is non-zero, ...

    Also, if you have no specific stream input for the process, I think it
    would be a good idea to close your OutputStream to it, though that
    should only matter if the process tries to read its input.

    --
    ss at comp dot lancs dot ac dot uk
     
    Steven Simpson, Mar 30, 2013
    #1
    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. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,073
    Smokey Grindel
    Dec 2, 2006
  2. Barb Knox
    Replies:
    1
    Views:
    264
    Steven Simpson
    Mar 30, 2013
  3. markspace
    Replies:
    1
    Views:
    267
    Arne Vajhøj
    Mar 30, 2013
  4. Roedy Green
    Replies:
    0
    Views:
    234
    Roedy Green
    Mar 30, 2013
  5. RVic
    Replies:
    2
    Views:
    259
    Roedy Green
    Apr 1, 2013
Loading...

Share This Page