ExpectJ can be used for automating interaction with either a process (through stdin / stdout) or a telnet session.

It is a Java implementation of the Unix expect utility.

Here's an example for how a short shell session can be automated. A TimeoutException will be thrown on timeout of one of the expect() calls.

// Create a new ExpectJ object with a timeout of 5s
ExpectJ expectinator = new ExpectJ(5);

// Fork the process
Spawn shell = expectinator.spawn("/bin/sh");

// Talk to it
shell.send("echo Chunder\n");
shell.expect("Chunder");
shell.send("exit\n");
shell.expectClose();

// Done!