rjb: Calling java methods from Ruby with compiled files in several directories
I have a Java library compiled in two directories:
Directory A
com.foo.bar.app.* //without test
Directory B
com.foo.bar.app.test.*
My objective, it is to call some simple java methods of com.foo.bar.app.test (wit开发者_如何学Pythonh dependencies in the directory A) using the rjb gem.
In the examples, they instance with this:
Rjb::load(classpath = '.', jvmargs=[])
How can i use the rjb to call a method methodFromCreate()
from a class com.foo.bar.app.test.create?
You could use something like:
require 'rjb'
RJB_LOAD_PATH = ["Directory A", "Directory B"].join(File::PATH_SEPARATOR)
RJB_OPTIONS = ['-Djava.awt.headless=true','-Xms16m', '-Xmx32m']
Rjb::load RJB_LOAD_PATH, RJB_OPTIONS
my_create_class = Rjb::import('com.foo.bar.app.test.Create')
my_create = my_create_class.new
my_create.methodFromCreate()
I added de RJB_OPTIONS we are using at the moment just for exemplification, if you need any awt stuff remove dthe -Djava.awt,... option.
I don't know about the rjb gem, but JRuby does this quite easily
Inside your ruby code you need to require java and add the path to your class hierarchy to the classpath. If you import the class, you can create an instance by calling new on the classname. If you do not import the class, you can create an instance by calling new on the fully qualified class name.
require 'java'
$CLASSPATH<< "path/to/java/classes";
import com.foo.bar.app.Class1
c1 = Class1.new
c2 = com.foo.bar.app.test.Class2.new
精彩评论