import java.io.*; public class Fork { public static void main( String[] args ) { try { Runtime env = Runtime.getRuntime(); Process p = env.exec( "ls -la" ); // parent does not wait for the child process to terminate System.out.println( "I'm the parent. Child process is running..." ); int rc = p.waitFor(); // parent waits for child to terminate if ( rc == 0 ) { System.out.println( "...terminated successfully." ); } else { System.out.println( "...terminated with error " + rc ); } BufferedReader stdout = new BufferedReader( new InputStreamReader( p.getInputStream() ) ); BufferedReader stderr = new BufferedReader( new InputStreamReader( p.getErrorStream() ) ); String s; System.out.println( "I'm the parent. Child process output:" ); while ( ( s = stdout.readLine() ) != null ) { System.out.println( s ); } System.out.println( "I'm the parent. Child process error output:" ); while ( ( s = stderr.readLine() ) != null ) { System.out.println( s ); } } catch ( Exception ex ) { System.err.println( "Exception: " + ex.getMessage() ); } } }