27
Jun
09

great jar : ExpectJ



I’ve found a Java implementation of Tcl’s expect at http://expectj.sourceforge.net/. I wrote the following groovy script to test it :


System.in.withReader { r ->
	println "age please:"
	def age = Integer.parseInt(r.readLine())
	if(age < 20) {
		println "too young"
		System.exit(0);
	}
	println "you can go! name please"
	def name = r.readLine()
	println name
}

and here’s the code that uses ExpectJ to send input :


import expectj.ExpectJ

ExpectJ ex = new ExpectJ(10)
def shell = ex.spawn("groovy groovysimo.groovy")
shell.expect("age please:")
Thread.sleep(1000)
shell.send("25\n")
shell.expect("name please")
shell.send("geo\n")
shell.expectClose()
print shell.exitValue

It’s amazing how well it works!

Advertisements

2 Responses to “great jar : ExpectJ”


  1. 1 josefbetancourt
    August 19, 2010 at 05:55

    That is amazing. How did you actually run this? I copied your code into two groovy scripts and when run in a Windows7 shell all I get is:

    P:\>groovy expect.groovy
    Caught: java.io.IOException: Cannot run program “groovy”: CreateProcess error=2, The system cannot find the file specified
    at expect.run(expect.groovy:5)


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Blog Stats

  • 223,848 hits

%d bloggers like this: