Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Process p = Runtime.getRuntime().exec(String cmd) not reading input stream on Linux

Reply
Thread Tools

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

 
 
nmlaney@gmail.com
Guest
Posts: n/a
 
      09-20-2006
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();

 
Reply With Quote
 
 
 
 
Matt Humphrey
Guest
Posts: n/a
 
      09-20-2006

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> 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 http://www.velocityreviews.com/forums/(E-Mail Removed) http://www.iviz.com/


 
Reply With Quote
 
 
 
 
danharrisandrews@gmail.com
Guest
Posts: n/a
 
      09-20-2006
(E-Mail Removed) wrote:
> 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/j...229-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
- - - - - - - - - - - - - - - - - - - - - - - - - - - -

 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      09-21-2006
On 20 Sep 2006 13:12:28 -0700, (E-Mail Removed) wrote:
> 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

--
[ don't email me support questions or followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
 
Reply With Quote
 
vivek_kothari vivek_kothari is offline
Junior Member
Join Date: Jan 2010
Posts: 2
 
      01-12-2010
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();
}
}
}
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Conversion from Input Stream to Output Stream Kashif Ur Rehman Java 2 05-17-2007 07:50 PM
How to GET multi-word input from a *file* stream as opposed to a *console* stream? sherifffruitfly@gmail.com C++ 9 04-27-2006 04:14 PM
Process causes its input stream to block (?) Robert J. Hansen Java 1 04-09-2006 09:13 AM
Process does not receive EOF when reading from Process.getOutputStream() Reid Madsen Java 8 05-16-2004 01:37 AM
Re: Reading the first three characters from an input stream Andrew Thompson Java 1 08-13-2003 12:36 PM



Advertisments