Problem with multiple pipes in script executed from Runtime.exec()

J

jeremy redburn

Hello,

I was wondering if anyone was aware of any limitations of shell
scripts run via java.lang.Runtime.exec? I'm trying to write a simple
Java program that runs a shell script and grabs the resultant output
as XML. For now, I can't even get my program to just take in the
output of the script and send it to stdout. It seems to work fine as
long as I don't have multiply piped lines in the shell script. Does
anyone have any advice?

The relevant part of the Java program:

Runtime rt = Runtime.getRuntime();
try {
Process process = rt.exec(cmd);
InputStream is = process.getInputStream();
byte[] buf = new byte[1024];
int l = 1;
while (l > 0) {
l = is.read(buf);
if (l > 0) {
System.out.write(buf, 0, l);
}
}
} catch (Exception e) {
System.err.println("Error: " + e);
}


myscript.sh just consists of:
#! /bin/sh
echo "Hello and hi" | grep "and" | sed 's/hi/goodbye/'
exit

When I run the Java program I get no output at all and I have to hit
Ctrl-C to regain control.

Thanks much,
Jeremy
 
S

Sudsy

jeremy said:
Hello,

I was wondering if anyone was aware of any limitations of shell
scripts run via java.lang.Runtime.exec? I'm trying to write a simple
Java program that runs a shell script and grabs the resultant output
as XML. For now, I can't even get my program to just take in the
output of the script and send it to stdout. It seems to work fine as
long as I don't have multiply piped lines in the shell script. Does
anyone have any advice?

The relevant part of the Java program:

Runtime rt = Runtime.getRuntime();
try {
Process process = rt.exec(cmd);
InputStream is = process.getInputStream();
byte[] buf = new byte[1024];
int l = 1;
while (l > 0) {
l = is.read(buf);
if (l > 0) {
System.out.write(buf, 0, l);
}
}
} catch (Exception e) {
System.err.println("Error: " + e);
}


myscript.sh just consists of:
#! /bin/sh
echo "Hello and hi" | grep "and" | sed 's/hi/goodbye/'
exit

So cmd = "/bin/sh -c myscript.sh", right?
 
M

Matt Humphrey

jeremy redburn said:
Hello,

I was wondering if anyone was aware of any limitations of shell
scripts run via java.lang.Runtime.exec? I'm trying to write a simple
Java program that runs a shell script and grabs the resultant output
as XML. For now, I can't even get my program to just take in the
output of the script and send it to stdout. It seems to work fine as
long as I don't have multiply piped lines in the shell script. Does
anyone have any advice?

The relevant part of the Java program:

Runtime rt = Runtime.getRuntime();
try {
Process process = rt.exec(cmd);
InputStream is = process.getInputStream();
byte[] buf = new byte[1024];
int l = 1;
while (l > 0) {
l = is.read(buf);
if (l > 0) {
System.out.write(buf, 0, l);
}
}
} catch (Exception e) {
System.err.println("Error: " + e);
}


myscript.sh just consists of:
#! /bin/sh
echo "Hello and hi" | grep "and" | sed 's/hi/goodbye/'
exit

When I run the Java program I get no output at all and I have to hit
Ctrl-C to regain control.

Are you sure your command is being run at all? Does it rely on an external
path that may not be set? Don't forget the big bugbear of running all
scripts--you must simultaneously (in threads) read standard out and standard
error. That is, if your script generates any stderr output and you don't
read it, the script may block until you do. Of course, your Java program
will be waiting for input, which will then never happen and you get
deadlock. In your case, a faulty cmd expression may be generating stderr
output that you don't read, so the script blocks and you block trying to
read from a waiting process.

If you search an archive of this newsgroup you'll find this issue has
appeared before and you should be able to get some sample code for reading
the streams.

Good luck,
Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
J

jeremy redburn

I'm sure the command is being written since I can output up to the
statement including the pipes and it shows up fine but nothing
afterwards. Also, the script doesn't generate anything on stderr
normally, but I'll check to make sure it doesn't when run from the
Java code.

Matt Humphrey said:
jeremy redburn said:
Hello,

I was wondering if anyone was aware of any limitations of shell
scripts run via java.lang.Runtime.exec? I'm trying to write a simple
Java program that runs a shell script and grabs the resultant output
as XML. For now, I can't even get my program to just take in the
output of the script and send it to stdout. It seems to work fine as
long as I don't have multiply piped lines in the shell script. Does
anyone have any advice?

The relevant part of the Java program:

Runtime rt = Runtime.getRuntime();
try {
Process process = rt.exec(cmd);
InputStream is = process.getInputStream();
byte[] buf = new byte[1024];
int l = 1;
while (l > 0) {
l = is.read(buf);
if (l > 0) {
System.out.write(buf, 0, l);
}
}
} catch (Exception e) {
System.err.println("Error: " + e);
}


myscript.sh just consists of:
#! /bin/sh
echo "Hello and hi" | grep "and" | sed 's/hi/goodbye/'
exit

When I run the Java program I get no output at all and I have to hit
Ctrl-C to regain control.

Are you sure your command is being run at all? Does it rely on an external
path that may not be set? Don't forget the big bugbear of running all
scripts--you must simultaneously (in threads) read standard out and standard
error. That is, if your script generates any stderr output and you don't
read it, the script may block until you do. Of course, your Java program
will be waiting for input, which will then never happen and you get
deadlock. In your case, a faulty cmd expression may be generating stderr
output that you don't read, so the script blocks and you block trying to
read from a waiting process.

If you search an archive of this newsgroup you'll find this issue has
appeared before and you should be able to get some sample code for reading
the streams.

Good luck,
Matt Humphrey (e-mail address removed) http://www.iviz.com/
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top