开发者

Can a Ruby app call Java code?

This is probably really simple, but all my Google results came back with JRuby, which isn't what I want to know. I'm curious if a regular Ruby app (e.g开发者_JAVA技巧. a Rails app or a Sinatra app) could somehow be made to reference and call a Java library that's in the classpath? Ideally one that works on Heroku.


Not directly. Java libraries run within the JVM, Ruby apps in their own VM. If you want those two to communicate, you will need to create some kind of a comms channel between them (there are various solutions, see f.e. http://code.google.com/p/activemessaging/).


I succeeded calling my Java program like this from Rails controller:

system "java -cp postgresql-9.0-801.jdbc4.jar:./ Main"

Couple of things to note:

  • I've compiled my Main class and put it in root of my rails app (and git-pushed it to heroku).
  • This call blocks rails app until java program finishes execution
  • It would probably work better with delayed_job gem (I'm working on it now)


  1. http://rjb.rubyforge.org/
  2. another option would be if the jar is executable and do something like execute system command and run it in a 'session'/terminal


You can perform the block of commands inline of ruby (jruby) file:

importString = <<-eos
  java -version
  ls -l
eos

exec importString

This OK for Mac, but Windows jruby has some limitations.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜