Process p = Runtime.getRuntime().exec(String cmd) not reading input stream on Linux

N

nmlaney

I'm attempting to use this code on Linux, and only an empty String is
being logged. I tried this with String line = input.readline() and I'm
getting other debug stmts. I get the response on Windows, but not
Linux. What am I missing?

Process p = Runtime.getRuntime().exec(cmd);
BufferedReader input = new BufferedReader(new
InputStreamReader(p.getInputStream()));
int ch;
StringBuffer sb = new StringBuffer("");
while ((ch = input.read()) != -1) {
sb.append(ch);
}
logger.debug(sb.toString());
input.close();
 
M

Matt Humphrey

I'm attempting to use this code on Linux, and only an empty String is
being logged. I tried this with String line = input.readline() and I'm
getting other debug stmts. I get the response on Windows, but not
Linux. What am I missing?

Process p = Runtime.getRuntime().exec(cmd);
BufferedReader input = new BufferedReader(new
InputStreamReader(p.getInputStream()));
int ch;
StringBuffer sb = new StringBuffer("");
while ((ch = input.read()) != -1) {
sb.append(ch);
}
logger.debug(sb.toString());
input.close();

There could be several things wrong here, although if you're sure it writes
a blank entry to the log (ie timestamp and no data) it seems likely that the
input stream actually contains nothing and does complete. It would guess
that it's giving you an error message on stderr which you are not showing.
It's probably saying it can't find the program (exec has different $PATH),
or that you're expecting the cmd to perform a shell operation (e.g. piping
output, etc). Find out what the error message says and show us what you're
putting into cmd.

In general you must read both stdout and stderr simultaneously (google for
StreamGobbler) because if either fills the buffer it will block and create
deadlock as your program will be waiting for more input. This is in the
Java Gotcha's.

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

danharrisandrews

I'm attempting to use this code on Linux, and only an empty String is
being logged. I tried this with String line = input.readline() and I'm
getting other debug stmts. I get the response on Windows, but not
Linux. What am I missing?

It has been awhile but I remember needing to read both stdin and
stderr. I've done a quite google and I'm going to direct you to this
link (http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html)
for starters. The reading in the good example is done in separate
threads for stdin and stderr. Good luck.

Cheers,

Dan Andrews
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
Ansir Development Limited http://www.ansir.ca
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
 
G

Gordon Beaton

I'm attempting to use this code on Linux, and only an empty String
is being logged. I tried this with String line = input.readline()
and I'm getting other debug stmts. I get the response on Windows,
but not Linux. What am I missing?

Others have already addressed the need to read from both streams
(output and error). I'll add that ProcessBuilder lets you combine them
into one, which could make things easier for the Java application.

Also, I'm curious what your command line looks like. Do any of the
command arguments contain spaces or otherwise need quoting? Are you
passing wildcards to the command, or doing anything else that requires
a shell? Are you starting a shell? Post a concrete example.

/gordon
 
Joined
Jan 12, 2010
Messages
2
Reaction score
0
How To Give Input To Process Created By Runtime.getRuTime().exec

Hi
I am Trying to run a c program through JAVA. The C Program is

#include
int main()
{
printf("Enter a number:\n");
}

and the JAVA code for invoking is

import java.io.*;
public class TimePass implements Runnable{
Process pro;
BufferedReader inputReaderForProcess;
BufferedReader errorReaderForProcess;
BufferedReader inputReaderForUser;
Thread tProcessInput,tProcessError,tProcessUserInput;
BufferedOutputStream wr;
public TimePass(){
tProcessInput=new Thread(this);
tProcessError=new Thread(this);
tProcessUserInput=new Thread(this);
try {
//pro=Runtime.getRuntime().exec("sh /home/globus/workspace/TimePass/bin/asd.sh");
pro=Runtime.getRuntime().exec("/home/globus/my");
/* try {
pro.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}*/
} catch (IOException e) {
e.printStackTrace();
}
tProcessInput.start();
tProcessError.start();
tProcessUserInput.start();
}
public static void main(String[] args) {
new TimePass();
}
@Override
public void run() {
String line;
if(Thread.currentThread()==tProcessError){
errorReaderForProcess=new BufferedReader(new InputStreamReader(pro.getErrorStream()));
try {
while((line=errorReaderForProcess.readLine())!=nul l){
line="ERROR > "+ line;
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
if(Thread.currentThread()==tProcessInput){
inputReaderForProcess=new BufferedReader(new InputStreamReader(pro.getInputStream()));
try {
while((line=inputReaderForProcess.readLine())!=nul l){
line="OUTPUT > "+ line;
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
if(Thread.currentThread()==tProcessUserInput){
wr=new BufferedOutputStream(pro.getOutputStream());
try {
wr.write(0);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top