Can a Ruby application invoke Java code?

It's probably very simple, but all of my Google results are back with JRuby, which I don't want to know. I'm curious if a regular Ruby application (such as a Rails application or a Sinatra application) can somehow make a link and call the Java library that is in the classpath? Ideally one that works on Geroku.

+4
source share
4 answers

Not directly. Java libraries run in Ruby JVM applications on their own virtual machine. If you want these two people to communicate, you need to create some kind of communication channel between them (there are various solutions, see Fe http://code.google.com/p/activemessaging/ ).

+5
source

I managed to call my Java program, like this from a Rails controller:

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

A few notes:

  • I compiled my main class and put it in the root of my rails application (and git put it in the hero).
  • This call blocks the rails application until the Java program completes.
  • Most likely, this will work better with delayed_job gem (I'm working on it now)
+3
source
+1
source

You can execute the command block in the ruby ​​(jruby) file line:

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

This is fine for a Mac, but Windows jruby has some limitations.

0
source

Source: https://habr.com/ru/post/1347792/


All Articles