H
Hendrik Maryns
Hi,
I call a program with ProcessBuilder. From
http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=4
I copied over the class StreamGobbler, to redirect the output. However,
sometimes this doesn’t work. I have to do the whole process twice, then
it works. I’m out of ideas what the cause could be, and don’t know how
to debug this. The SSCCE I extracted does not work at all, in that it
doesn’t write anything to the output file. It does write to the
console, though.
I read Roedy’s page on exec() and more.
Any ideas mostly appreciated. Classes found below.
==StreamGobbler.java==
import java.io.*;
public class StreamGobbler extends Thread {
private final InputStream is;
private final String type;
private final OutputStream os;
public StreamGobbler(InputStream is, String type, OutputStream
redirect) {
this.is = is;
this.type = type + ">";
os = redirect;
}
@Override
public void run() {
try {
PrintWriter pw = null;
if (os != null) {
pw = new PrintWriter(os);
}
final InputStreamReader isr = new InputStreamReader(is);
final BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
if (pw != null) {
pw.println(line);
}
System.out.println(type + line);
}
if (pw != null) {
pw.flush();
}
} catch (final IOException ioe) {
ioe.printStackTrace();
}
}
}
==MonaCaller.java==
import java.io.*;
public class MonaCaller {
public static void main(String[] args) throws IOException,
FileNotFoundException, InterruptedException {
final File automFile = new File("test.out");
final ProcessBuilder monaCaller = new ProcessBuilder("echo",
"test");
final Process mona = monaCaller.start();
final StreamGobbler monaError = new StreamGobbler(
mona.getErrorStream(), "test error", null);
final FileOutputStream monaAutomaton = new
FileOutputStream(automFile);
final StreamGobbler monaOutput = new StreamGobbler(mona
.getInputStream(), "test output", monaAutomaton);
monaError.start();
monaOutput.start();
mona.waitFor();
monaAutomaton.close();
}
}
TIA, H.
--
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.4-svn0 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org
iD8DBQFHXUxee+7xMGD3itQRAlt8AJwK20PjepX227c1lNOgEHp04/fOGQCfbbUV
ximTMkAGpAU1JKCnfO4Rado=
=Y3XA
-----END PGP SIGNATURE-----
I call a program with ProcessBuilder. From
http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=4
I copied over the class StreamGobbler, to redirect the output. However,
sometimes this doesn’t work. I have to do the whole process twice, then
it works. I’m out of ideas what the cause could be, and don’t know how
to debug this. The SSCCE I extracted does not work at all, in that it
doesn’t write anything to the output file. It does write to the
console, though.
I read Roedy’s page on exec() and more.
Any ideas mostly appreciated. Classes found below.
==StreamGobbler.java==
import java.io.*;
public class StreamGobbler extends Thread {
private final InputStream is;
private final String type;
private final OutputStream os;
public StreamGobbler(InputStream is, String type, OutputStream
redirect) {
this.is = is;
this.type = type + ">";
os = redirect;
}
@Override
public void run() {
try {
PrintWriter pw = null;
if (os != null) {
pw = new PrintWriter(os);
}
final InputStreamReader isr = new InputStreamReader(is);
final BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
if (pw != null) {
pw.println(line);
}
System.out.println(type + line);
}
if (pw != null) {
pw.flush();
}
} catch (final IOException ioe) {
ioe.printStackTrace();
}
}
}
==MonaCaller.java==
import java.io.*;
public class MonaCaller {
public static void main(String[] args) throws IOException,
FileNotFoundException, InterruptedException {
final File automFile = new File("test.out");
final ProcessBuilder monaCaller = new ProcessBuilder("echo",
"test");
final Process mona = monaCaller.start();
final StreamGobbler monaError = new StreamGobbler(
mona.getErrorStream(), "test error", null);
final FileOutputStream monaAutomaton = new
FileOutputStream(automFile);
final StreamGobbler monaOutput = new StreamGobbler(mona
.getInputStream(), "test output", monaAutomaton);
monaError.start();
monaOutput.start();
mona.waitFor();
monaAutomaton.close();
}
}
TIA, H.
--
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.4-svn0 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org
iD8DBQFHXUxee+7xMGD3itQRAlt8AJwK20PjepX227c1lNOgEHp04/fOGQCfbbUV
ximTMkAGpAU1JKCnfO4Rado=
=Y3XA
-----END PGP SIGNATURE-----