2

I am currently writing a ruby application that requires the output of a java jar.

I currently can not get any output from the java command using open3.

I have however, successfully tested the following ruby code using 'ls'.

require 'open3'
 Open3.popen3('java -version') do |stdrin, stdout, stderr|
   @output = stdout.read
 end

I have also tried using an absolute path for java: '/opt/java/jre/bin/java' with no success.

Thank you, in advance

Rick Barrette
  • 272
  • 4
  • 19
  • Maybe there is output waiting to be read from stderr – theglauber Apr 27 '12 at 16:41
  • As [Niklas B.](http://stackoverflow.com/users/916657/niklas-b) has answered, `java -version` writes to `STDERR`. If this doesn't explain the problem you're having then please update the question to show something closer to what the jar you're trying to execute does. – mikej Apr 27 '12 at 16:44
  • Yep... i'm guessing the command with the jar file outputted an error (either it didn't find the jar file, or some other thing went wrong, possibly something missing in CLASSPATH). Can't tell for sure without more info. – theglauber Apr 27 '12 at 16:48

2 Answers2

4

java -version writes to STDERR, so the following should work:

@output = stderr.read

On Linux, you can check in the shell where the output is going by supressing either STDERR or STDOUT:

java -version >/dev/null

still prints the information.

Niklas B.
  • 92,950
  • 18
  • 194
  • 224
0

Do you need open3, specifically? In Ruby 1.9, i would do:

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

But there is often a lot more to this to running a Java program (setting up the CLASSPATH, for example); so you may be better off running a shell script that runs the Java program.

theglauber
  • 28,367
  • 7
  • 29
  • 47