Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #23162

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

Path csiph.com!usenet.pasdenom.info!gegeweb.org!eternal-september.org!feeder.eternal-september.org!mx05.eternal-september.org!.POSTED!not-for-mail
From Steven Simpson <ss@domain.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Why does this only work when I am running a shellscript
Date Sat, 30 Mar 2013 16:07:22 +0000
Organization A noiseless patient Spider
Lines 48
Message-ID <q6fj2a-oog.ln1@s.simpson148.btinternet.com> (permalink)
References <8738vdmg0h.fsf@Servus.decebal.nl> <see-8C9CC4.03544931032013@news.eternal-september.org>
Mime-Version 1.0
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 8bit
Injection-Info mx05.eternal-september.org; posting-host="7e194865abb1454ff7685570b1bd8709"; logging-data="21418"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/T7yolWQ2xMFrFAv59BAa5ys3AqlOD7vo="
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130308 Thunderbird/17.0.4
In-Reply-To <see-8C9CC4.03544931032013@news.eternal-september.org>
Cancel-Lock sha1:/fRsMltkTQTeUw2uNQcDbgnLyQ8=
Xref csiph.com comp.lang.java.programmer:23162

Show key headers only | View raw


On 30/03/13 14:54, Barb Knox wrote:
> In article <8738vdmg0h.fsf@Servus.decebal.nl>,
>   Cecil Westerhof <Cecil@decebal.nl> 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

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Why does this only work when I am running a shellscript Cecil Westerhof <Cecil@decebal.nl> - 2013-03-30 09:46 +0100
  Re: Why does this only work when I am running a shellscript Barb Knox <see@sig.below> - 2013-03-31 03:54 +1300
    Re: Why does this only work when I am running a shellscript Steven Simpson <ss@domain.invalid> - 2013-03-30 16:07 +0000
  Re: Why does this only work when I am running a shellscript markspace <markspace@nospam.nospam> - 2013-03-30 08:23 -0700
    Re: Why does this only work when I am running a shellscript Arne Vajhøj <arne@vajhoej.dk> - 2013-03-30 11:34 -0400
  Re: Why does this only work when I am running a shellscript Roedy Green <see_website@mindprod.com.invalid> - 2013-03-30 08:35 -0700
  Re: Why does this only work when I am running a shellscript RVic <rvince99@hotmail.com> - 2013-03-30 08:41 -0700
    Re: Why does this only work when I am running a shellscript Joerg Meier <joergmmeier@arcor.de> - 2013-03-31 01:00 +0100
    Re: Why does this only work when I am running a shellscript Roedy Green <see_website@mindprod.com.invalid> - 2013-03-31 17:04 -0700
  Re: Why does this only work when I am running a shellscript Steven Simpson <ss@domain.invalid> - 2013-03-30 16:13 +0000

csiph-web