Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Reading from a Process InputStream!

2 views
Skip to first unread message

Karl Lopes

unread,
May 20, 1998, 3:00:00 AM5/20/98
to

Hello All,
I have written a program which is supposed to execute
some native command and then display the result in a text area.
The program works just dandy on Unix but I get the following error
on Win95/NT.
java.io.IOException: CreateProcess: dir error=0
at java.lang.Win32Process.create(Native Method)
at java.lang.Win32Process.<init>(Win32Process.java:53)
at java.lang.Runtime.execInternal(Native Method)
at java.lang.Runtime.exec(Runtime.java:162)
at java.lang.Runtime.exec(Runtime.java:125)
at test.execute(test.java:10)
at test.main(test.java:40)

Method which is being used:
public void execute()throws RuntimeException, SecurityException, IOException
{
Process p = Runtime.getRuntime().exec(command);

InputStream er = p.getErrorStream();

BufferedReader os = new BufferedReader( new InputStreamReader(p.getInputStream()) );


String output = "";
while((output = os.readLine())!=null)
{
System.out.println( os );
messages.setText(output+'\n');

}

String erronous = "";
BufferedReader ir = new BufferedReader( new InputStreamReader( er ) );
if( ir == null )
return;
while((erronous = ir.readLine())!=null)
{
errors.setText(erronous+'\n');
}
}
The program throws an error at the very first line of the method.
Any suggestions
--Karl.

Philip Bierhoff

unread,
May 21, 1998, 3:00:00 AM5/21/98
to

Karl Lopes (lo...@cs.buffalo.edu) wrote:
: Hello All,

Yes, it's not in your code, but I suppose you are passing "dir" as command.
"dir" is not a executable program on Windows, but part of the command.com
program. Try "command.com /c dir" as argument, this is supposed to do the
trick, or try something that IS a program ("tree" is, as far as I know).

Philip
: --Karl.

Peter Janson

unread,
May 21, 1998, 3:00:00 AM5/21/98
to Karl Lopes
It looks like you´re trying to give the dir command. Am I right?
You are probably trying to do this like this:
Process p = Runtime.getRuntime().exec("dir");

This doesn´t work because dir is a internal dos command (i.e. not found in the path).
You should do it like this instead (I think one the commands below works for Win95 and the other for WinNT):
Process p = Runtime.getRuntime().exec("command /c dir");
Process p = Runtime.getRuntime().exec("cmd /c dir");

If you want more info check out Java Developer Connection. There has been some questions and bug reports dealing with exec() and Win95.

Hope this helps you!

/Peter

Karl Lopes wrote:

        InputStream er = p.getErrorStream();

        }

        --Karl.

 

Allan Wax

unread,
May 21, 1998, 3:00:00 AM5/21/98
to

Try adding a waitFor(). Something like this.

Runtime rt = Runtime.getRuntime();
Process child = rt.exec ("dir");
try {
System.err.println("Return status from call is " +
child.waitFor());
DataInputStream in = new DataInputStream(new
BufferedInputStream(child.getInputStream()));

while (in.available() > 0) {
System.out.println(in.read());
}


===

--
<------------------------------------------->
Allan Wax
e-mail: Alla...@jpl.nasa.gov
FAX: + (213) 935-9292

NOTE: The views expressed in this message are
my own and do not necessarily reflect
those of my employer.

0 new messages