rjb:使用几个目录中的已编译文件从Ruby调用java方法



我有一个编译在两个目录中的Java库:

Directory A
   com.foo.bar.app.* //without test
Directory B
   com.foo.bar.app.test.*

我的目标是使用rjbgem调用com.foo.bar.app.test的一些简单的java方法(在目录A中有依赖项)。

在这些例子中,他们的例子是:

Rjb::load(classpath = '.', jvmargs=[])

如何使用rjb从类com.foo.bar.app.test.create中调用方法methodFromCreate()

您可以使用以下内容

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()

我添加了我们目前使用的de RJB_OPTIONS,只是为了举例说明,如果你需要任何awt的东西,请删除Djava.awt,…选项。

我不知道rjbgem,但JRuby很容易做到

在ruby代码中,您需要java,并将类层次结构的路径添加到类路径中。如果导入类,则可以通过对类名调用new来创建实例。如果不导入类,则可以通过对完全限定的类名调用new来创建实例。

require 'java'
$CLASSPATH<< "path/to/java/classes";  
import com.foo.bar.app.Class1
c1 = Class1.new
c2 = com.foo.bar.app.test.Class2.new

最新更新