R
rwfields
Attached is an example of how to grab stdout and stderr from a spawned
process. Comments are welcome.
Randall
/* Exec.java */
import java.io.IOException;
/** A example of spawning a process. */
public class Exec
{
public static void main (String[] args)
{
System.out.println("Hello world!");
String[] command = {
"C:/Sun/jdk1.5.0_06/bin/java.exe",
"HelloLog"
};
try
{
Process process = Runtime.getRuntime().exec(command);
StreamConsumer inFromProcess
= new StreamConsumer(process.getInputStream());
inFromProcess.start();
StreamConsumer errFromProcess
= new StreamConsumer(process.getErrorStream());
errFromProcess.start();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
/* StreamConsumer.java */
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.io.IOException;
public class StreamConsumer extends Thread
{
protected BufferedInputStream stream;
protected int bufSize = 1024;
protected byte[] buffer;
public StreamConsumer(InputStream stream)
{
this.stream = new BufferedInputStream(stream);
buffer = new byte[bufSize];
}
public void run()
{
boolean isDone = false;
while ( !isDone )
{
try
{
int count = stream.read(buffer, 0, bufSize);
String out = new String(buffer);
System.out.print(out);
if ( count < 0 )
{
isDone = true;
}
}
catch(IOException e)
{
isDone = true;
e.printStackTrace();
}
}
}
}
/* HelloLog.java */
import java.util.logging.Logger;
/** A class that prints something to stderr. */
public class HelloLog
{
private static final Logger log
= Logger.getLogger(HelloLog.class.getName());
public static void main (String[] args)
{
for ( int i = 0; i < 10; i++ )
{
log.info("Hello world!");
}
}
}
process. Comments are welcome.
Randall
/* Exec.java */
import java.io.IOException;
/** A example of spawning a process. */
public class Exec
{
public static void main (String[] args)
{
System.out.println("Hello world!");
String[] command = {
"C:/Sun/jdk1.5.0_06/bin/java.exe",
"HelloLog"
};
try
{
Process process = Runtime.getRuntime().exec(command);
StreamConsumer inFromProcess
= new StreamConsumer(process.getInputStream());
inFromProcess.start();
StreamConsumer errFromProcess
= new StreamConsumer(process.getErrorStream());
errFromProcess.start();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
/* StreamConsumer.java */
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.io.IOException;
public class StreamConsumer extends Thread
{
protected BufferedInputStream stream;
protected int bufSize = 1024;
protected byte[] buffer;
public StreamConsumer(InputStream stream)
{
this.stream = new BufferedInputStream(stream);
buffer = new byte[bufSize];
}
public void run()
{
boolean isDone = false;
while ( !isDone )
{
try
{
int count = stream.read(buffer, 0, bufSize);
String out = new String(buffer);
System.out.print(out);
if ( count < 0 )
{
isDone = true;
}
}
catch(IOException e)
{
isDone = true;
e.printStackTrace();
}
}
}
}
/* HelloLog.java */
import java.util.logging.Logger;
/** A class that prints something to stderr. */
public class HelloLog
{
private static final Logger log
= Logger.getLogger(HelloLog.class.getName());
public static void main (String[] args)
{
for ( int i = 0; i < 10; i++ )
{
log.info("Hello world!");
}
}
}