Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Could someone help me with class Process getErrorStream() ??

807591Jun 28 2006 — edited Mar 3 2008
Hello
I want to execute process "/usr/bin/octave" .So I have to write to its input and read its output and error stream. I have written program and it works good until I need to read error stream of process. After I read the error stream of process i receive the IOException Broken pipe. It looks like after read the data from error stream process is closed so I coudn't write anything to the input stream (what i could do before reading from error stream). The errors I receive :

java.io.IOException: Broken pipe
at java.io.FileOutputStream.writeBytes(Native Method)
at java.io.FileOutputStream.write(Unknown Source)
at java.io.BufferedOutputStream.flushBuffer(Unknown Source)
at java.io.BufferedOutputStream.flush(Unknown Source)
at sun.nio.cs.StreamEncoder$CharsetSE.implFlush(Unknown Source)
at sun.nio.cs.StreamEncoder.flush(Unknown Source)
at java.io.OutputStreamWriter.flush(Unknown Source)
at java.io.BufferedWriter.flush(Unknown Source)
at wazna$3.run(octave.java:116)
at java.lang.Thread.run(Unknown Source)
And here is my code:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

public class octave{


public static void main(String[] args) throws IOException, InterruptedException {

Runtime r = Runtime.getRuntime();
final Process p= r.exec("/usr/bin/octave");
System.out.print(">>");
InputStream iss = p.getInputStream();
BufferedReader br =
new BufferedReader(new InputStreamReader(iss));
try {

while (iss.available() == 0) { }
while (br.ready())
{
System.out.println(br.readLine());
}
} catch (IOException e) { e.printStackTrace();}

String s=null;

do

{
System.out.print(">>");
BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));

s = in.readLine();

if (s.equalsIgnoreCase("exit"))
{
System.out.print(">>");
System.out.print("END");
}
else
{
Thread reading = new Thread(
new Runnable()
{
synchronized public void run()
{

InputStream iss = p.getInputStream();
BufferedReader br =
new BufferedReader(new InputStreamReader(iss));

try {


while (iss.available() == 0) { }

while (br.ready()) {

System.out.println(br.readLine());
}

} catch (Exception e) {e.printStackTrace(); } }
});
Thread error = new Thread(
new Runnable()
{
synchronized public void run()
{
InputStream err=p.getErrorStream();
BufferedReader er =
new BufferedReader(new InputStreamReader(err));

try {
while (err.available() == 0) {
}

while (er.ready()) {

System.err.println(er.readLine());
}


} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
final String g=s;


Thread writing = new Thread(
new Runnable()
{
synchronized public void run()
{

OutputStream oos = p.getOutputStream();

BufferedWriter bw =
new BufferedWriter(new OutputStreamWriter(oos));

try {

bw.flush();

bw.write(g);

bw.newLine();
bw.flush(); //LINE 116 where is an error


} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}

});





writing.start();
reading.start();
error.start();

}
}
while (!s.equalsIgnoreCase("exit"));









}

}

Message was edited by:
tazja

Message was edited by:
tazja
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 31 2008
Added on Jun 28 2006
1 comment
372 views