import java.io.*; public class Fork { public static void main( String[] args ) { try { Runtime env = Runtime.getRuntime(); // Process p = env.exec( "vi xyz.txt" ); // Process p = env.exec( "lsxyfew -l" ); Process p = env.exec( "ls -e" ); // parent does not wait for child System.out.println( "PARENT: child process is running..." ); BufferedReader stdin = new BufferedReader( new InputStreamReader( p.getInputStream() ) ); BufferedReader stderr = new BufferedReader( new InputStreamReader( p.getErrorStream() ) ); int rc = p.waitFor(); // block and wait for child to terminate if ( rc == 0 ) { System.out.println( "...child terminated normally" ); } else { System.err.println( "...child terminated abnormally with error " + rc ); } String s; System.out.println( "PARENT: child process output:" ); while ( ( s = stdin.readLine() ) != null ) { System.out.println( s ); } System.out.println( "PARENT: child process error output:" ); while ( ( s = stderr.readLine() ) != null ) { System.out.println( s ); } } catch ( Exception ex ) { System.err.println( "Exception: " + ex.getMessage() ); } } }