Archive for the 'expectj' Category

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!




Blog Stats

  • 218,505 hits