Search code examples
javapythonpexpect

Pexpect equivalent for java?


Is there a pexpect equivalent for java? I know that I could replicate the functionality by using commons exec or processbuilder and redirecting stdin/stdout and then parsing the streams etc etc But, my question is, is there a plug-and-play equivalent for that?

What I would like would be to run interactive command line programs like I do in python. A small example is:

def start_binary(input):
   shell_cmd = 'grep '+input+' file4.dat >> file3.dat'
   grep = pexpect.spawn('/bin/bash', ['-c', shell_cmd])
   grep.expect(pexpect.EOF)
   shell_cmd = './myProg -c'
   myProg = pexpect.spawn('/bin/bash', ['-c', shell_cmd])
   myProg.expect('Next DATA file')
   file_list = ['file1.dat', 'file2.dat', 'file3.dat']
   for this_file in file_list:
      myProg.sendline(this_file)
   myProg.sendline()
   return myProg

And then I use myProg in an interactive way (sending commands, getting/parsing results, etc). I can get the results between two calls through myProg.before, etc.

Is there a package that offers the same functionality in the same easy way in java?


Solution

  • A simple Google search for "java expect" found expect4j as the first item in the list and ExpectJ as the second item listed.