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

Discussion in 'Java' started by Barb Knox, Mar 30, 2013.

  1. Barb Knox

    Barb Knox Guest

    In article <>,
    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());
    > }
    > }
    >
    > When I call s shellscript like:
    > doCommand("createQuote.sh citation");
    >
    > It works as I would expect. When I use something like:
    > /usr/bin/convert Š
    >
    > It hangs. It is not the command, because if I execute the command I
    > print in the shell directly, there is no problem.
    >
    > So what is happening here?



    I have not tested this, but I expect the Scanner is blocking waiting for
    input. You might want to try while (sc.hasNextLine()).

    --
    ---------------------------
    | BBB b \ Barbara at LivingHistory stop co stop uk
    | B B aa rrr b |
    | BBB a a r bbb | Quidquid latine dictum sit,
    | B B a a r b b | altum videtur.
    | BBB aa a r bbb |
    -----------------------------
    Barb Knox, Mar 30, 2013
    #1
    1. Advertising

  2. On 30/03/13 14:54, Barb Knox wrote:
    > In article <>,
    > 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());
    >> }
    >> }
    >> When I use something like:
    >> /usr/bin/convert Š
    >>
    >> It hangs.

    > I have not tested this, but I expect the Scanner is blocking waiting for
    > input. You might want to try while (sc.hasNextLine()).


    It's probably good advice anyway, but I don't think it makes much
    difference in this case. hasNext() tests for the next 'token', using a
    whitespace separator by default. This means that it will block while
    there are only blank lines on the unclosed input. The next non-blank
    line will unblock it, then each of the pending blank lines will be read,
    while hasNext() remains true (because the next token is still not
    consumed). Blank lines followed by EOF will be skipped, however.

    I tested with the following program:

    import java.io.*;
    import java.util.*;

    public class Scan {
    public static void main(String[] args) throws Exception {
    Scanner s = new Scanner(System.in);
    while (s.hasNext()) {
    String l = s.nextLine();
    System.out.printf("Next line: %s<--%n", l);
    }
    }
    }

    --
    ss at comp dot lancs dot ac dot uk
    Steven Simpson, Mar 30, 2013
    #2
    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:
    1,756
    Smokey Grindel
    Dec 2, 2006
  2. markspace
    Replies:
    1
    Views:
    256
    Arne Vajhøj
    Mar 30, 2013
  3. Roedy Green
    Replies:
    0
    Views:
    227
    Roedy Green
    Mar 30, 2013
  4. RVic
    Replies:
    2
    Views:
    242
    Roedy Green
    Apr 1, 2013
  5. Steven Simpson
    Replies:
    0
    Views:
    231
    Steven Simpson
    Mar 30, 2013
Loading...

Share This Page