package com.mindprod.example;
import java.io.*;
/**
* Demonstrate how to spawn a non-Java utility and communicate with it.
* <p/>
* composed with IntelliJ IDEA
*
* @author Roedy Green, Canadian Mind Products
* @version 1.0, 2007-11-30
*/
@SuppressWarnings( { "UnusedAssignment" } )
public class Spawner
{
/**
* main method
*
* @param args not used
*/
public static void main( String[] args )
{
ProcessBuilder pb = new ProcessBuilder( "fictitious.exe", "E:/temp/temp.txt" );
pb.directory( new File( "E:/sys" ) );
pb.redirectErrorStream( true );
try
{
final Process p = pb.start();
new Thread()
{
@SuppressWarnings( { "EmptyCatchBlock" } )
public void run()
{
final InputStream is = p.getInputStream();
final InputStreamReader isr = new InputStreamReader( is );
final BufferedReader br = new BufferedReader( isr, 100);
String line;
try
{
try
{
while ( ( line = br.readLine() ) != null )
{
System.out.println( line );
}
}
catch ( EOFException e )
{
}
br.close();
}
catch ( IOException e )
{
System.err.println( "problem reading spawn output" + e.getMessage() );
System.exit( 1 );
}
}
}.start();
new Thread()
{
public void run()
{
final OutputStream os = p.getOutputStream();
final OutputStreamWriter osw = new OutputStreamWriter( os );
final BufferedWriter bw = new BufferedWriter( osw, 100);
String line;
try
{
bw.write( "some text\n" );
bw.write( "some more text\n" );
bw.close();
}
catch ( IOException e )
{
System.err.println( "problem writing spawn input" + e.getMessage() );
System.exit( 1 );
}
}
}.start();
try
{
p.waitFor();
}
catch ( InterruptedException e )
{
Thread.currentThread().interrupt();
}
p.getInputStream().close();
p.getOutputStream().close();
p.getErrorStream().close();
}
catch ( IOException e )
{
System.err.println( "spawning problem " + e.getMessage() );
System.exit( 1 );
}
}
}