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


Groups > comp.lang.java.programmer > #23155 > unrolled thread

Why does this only work when I am running a shellscript

Started byCecil Westerhof <Cecil@decebal.nl>
First post2013-03-30 09:46 +0100
Last post2013-03-30 16:13 +0000
Articles 10 — 8 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  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

#23155 — Why does this only work when I am running a shellscript

FromCecil Westerhof <Cecil@decebal.nl>
Date2013-03-30 09:46 +0100
SubjectWhy does this only work when I am running a shellscript
Message-ID<8738vdmg0h.fsf@Servus.decebal.nl>
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?

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

[toc] | [next] | [standalone]


#23156

FromBarb Knox <see@sig.below>
Date2013-03-31 03:54 +1300
Message-ID<see-8C9CC4.03544931032013@news.eternal-september.org>
In reply to#23155
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 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   |   
-----------------------------

[toc] | [prev] | [next] | [standalone]


#23162

FromSteven Simpson <ss@domain.invalid>
Date2013-03-30 16:07 +0000
Message-ID<q6fj2a-oog.ln1@s.simpson148.btinternet.com>
In reply to#23156
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

[toc] | [prev] | [next] | [standalone]


#23157

Frommarkspace <markspace@nospam.nospam>
Date2013-03-30 08:23 -0700
Message-ID<kj6vt9$3c7$1@dont-email.me>
In reply to#23155
On 3/30/2013 1:46 AM, 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());

Besides Barb's answer, I believe you also have to drain the output 
stream.  If you don't read a program's output, it'll hang, waiting for you.


[toc] | [prev] | [next] | [standalone]


#23158

FromArne Vajhøj <arne@vajhoej.dk>
Date2013-03-30 11:34 -0400
Message-ID<5157060d$0$32105$14726298@news.sunsite.dk>
In reply to#23157
On 3/30/2013 11:23 AM, markspace wrote:
> On 3/30/2013 1:46 AM, 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());
>
> Besides Barb's answer, I believe you also have to drain the output
> stream.  If you don't read a program's output, it'll hang, waiting for you.

He does attempt to read in a loop.

Arne

[toc] | [prev] | [next] | [standalone]


#23159

FromRoedy Green <see_website@mindprod.com.invalid>
Date2013-03-30 08:35 -0700
Message-ID<lh1el8pn1p2el8f844qf0ddacnd5ks67c1@4ax.com>
In reply to#23155
On Sat, 30 Mar 2013 09:46:38 +0100, Cecil Westerhof <Cecil@decebal.nl>
wrote, quoted or indirectly quoted someone who said :

>It hangs. It is not the command, because if I execute the command I
>print in the shell directly, there is no problem.

See http://mindprod.com/jgloss/exec.html

exec wants an executable, not a raw script command.
-- 
Roedy Green Canadian Mind Products http://mindprod.com
Motors make noise, and that tells you about the feelings and attitudes 
that went into it. Something was more important than sensory pleasure -- 
nobody would invent a chair or dish that smelled bad or that made horrible 
noises -- why were motors invented noisy? How could they possibly be 
considered complete or successful inventions with this glaring defect?
Unless, of course, the aggressive, hostile, assaultive sound actually served
to express some impulse of the owner. 
~ Philip Slater (born: 1927 age: 85)
The Wayward Gate: Science and the Supernatural

[toc] | [prev] | [next] | [standalone]


#23160

FromRVic <rvince99@hotmail.com>
Date2013-03-30 08:41 -0700
Message-ID<27244e13-5a99-496e-9c96-61fffea1a8ae@googlegroups.com>
In reply to#23155
Roedy, Isn't there a way for Java to "execute" a raw script? I recall doing this once but cannot find my old code.RVic

[toc] | [prev] | [next] | [standalone]


#23165

FromJoerg Meier <joergmmeier@arcor.de>
Date2013-03-31 01:00 +0100
Message-ID<na3l4l96zask$.1pbsgmtki3vo9.dlg@40tude.net>
In reply to#23160
On Sat, 30 Mar 2013 08:41:17 -0700 (PDT), RVic wrote:

> Roedy, Isn't there a way for Java to "execute" a raw script? I recall doing this once but cannot find my old code.RVic

You have replied to the wrong post - your own, instead of Roedys. That
makes your thread hard to read for those trying to help you.

You can execute a raw script by executing (ba)sh (or cmd.exe under Windows)
and using the script as a parameter.

That being said: if your script is properly formatted (meaning the first
line looks like #!/bin/bash or whatever), it should be no problem executing
it the way you do now.

Liebe Gruesse,
		Joerg

-- 
Ich lese meine Emails nicht, replies to Email bleiben also leider
ungelesen.

[toc] | [prev] | [next] | [standalone]


#23172

FromRoedy Green <see_website@mindprod.com.invalid>
Date2013-03-31 17:04 -0700
Message-ID<1ojhl8hakcsjqckfr5n973lvm6fuvrspdv@4ax.com>
In reply to#23160
On Sat, 30 Mar 2013 08:41:17 -0700 (PDT), RVic <rvince99@hotmail.com>
wrote, quoted or indirectly quoted someone who said :

>Roedy, Isn't there a way for Java to "execute" a raw script? I recall doing this once but cannot find my old code.RVic

 sure. Exec a command processor with the script as a parm. See
http://mindprod.com/jgloss/exec.html for examples.
-- 
Roedy Green Canadian Mind Products http://mindprod.com
Motors make noise, and that tells you about the feelings and attitudes 
that went into it. Something was more important than sensory pleasure -- 
nobody would invent a chair or dish that smelled bad or that made horrible 
noises -- why were motors invented noisy? How could they possibly be 
considered complete or successful inventions with this glaring defect?
Unless, of course, the aggressive, hostile, assaultive sound actually served
to express some impulse of the owner. 
~ Philip Slater (born: 1927 age: 85)
The Wayward Gate: Science and the Supernatural

[toc] | [prev] | [next] | [standalone]


#23161

FromSteven Simpson <ss@domain.invalid>
Date2013-03-30 16:13 +0000
Message-ID<qifj2a-vsg.ln1@s.simpson148.btinternet.com>
In reply to#23155
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web