0

I'm trying to execute a .jar file from ruby and read the output. The following solution works when my .jar file does not expect any parameters to be passed. But when I try to do this, I get an error -

pipe = IO.popen( [ '/path/to/java', '-jar', "/path/to/jarfile.jar #{argsA} #{argsB}", {SDTERR=>STDOUT} ] 
pipe.close_write 
puts pipe.readlines

I need to pass a couple of parameters to the .jar file. How can I acheive this? I've tried several ways to do this but wasn't successful. This is the error I get, where 'Hello' and 'World' are the arguments I'm passing -

Error: Unable to access jarfile jarfile.jar Hello World

Useful links: How to get java output with ruby

call jar file from ruby class

Community
  • 1
  • 1
Maddyjak
  • 13
  • 4

1 Answers1

0

Put argsA and argsB in their own parameter, otherwise when seeing spaces in the parameters, ruby will surround them with quotes, so they're considered like one.

pipe = IO.popen( [ '/path/to/java', '-jar', '/path/to/jarfile.jar',"#{argsA}", "#{argsB}", {SDTERR=>STDOUT} ] 
JP Moresmau
  • 7,388
  • 17
  • 31