Help getting Ruby and Java to interact?
March 24, 2008 10:26 PM   Subscribe

I'm trying to compile and run a Java class from Ruby, but I'm having some problems. I'm not very experienced with Ruby- please help me figure out what I'm doing wrong.

Basically what I'm trying to do is:

1. Inside a Ruby method, compile (`javac`)
2. Run Foo.class with arguments and save the 'console' output as a string. (result = `java Foo filename.txt`)

The command to compile works, as Foo.class appears in the folder. However, part 2 doesn't seem to work as nothing prints when I try to print result (puts result). I'm probably doing something simple wrong but I'm not sure what it is as I'm basically picking Ruby up as I go without any actual grounding.
posted by version control to Computers & Internet (7 answers total)
Perhaps you could copy and paste your code into this thread?
posted by Blazecock Pileon at 10:51 PM on March 24, 2008

Or at least just the relevant bits.
posted by Blazecock Pileon at 10:51 PM on March 24, 2008

Among possible other options, this is what JRuby was made for.
posted by rhizome at 10:53 PM on March 24, 2008 [1 favorite]

Try this to see whether the last backtick command succeeded:

puts $?.success?

You can also try running the whole thing interactively in irb, and then poking at the results. Type "$?.methods" for more ideas.

If this is on Windows, this thread might help you figure out why it's failing.
posted by mbrubeck at 11:00 PM on March 24, 2008

Instead of the backtick, try Open3#popen3. Whatever error messages you're getting are going to standard error, which you can get from popen3.

Also, try absolute paths in your command line for the java interpreter, the class file, and the input file.
posted by qxntpqbbbqxl at 11:11 PM on March 24, 2008

Well I feel stupid, apparently it couldn't find the input file. Thanks for all the suggestions!
posted by version control at 5:38 AM on March 25, 2008

Definitely use JRuby, as rhizome suggested above.
posted by fredoliveira at 11:43 AM on March 25, 2008

« Older Help me keep my new job!   |   orthopedic stumper Newer »
This thread is closed to new comments.