简体   繁体   English

如何使用Ruby Java Bridge导入类

[英]How to import classes with Ruby Java Bridge

I have a jar file that has a class that I would like to use from my Rails project. 我有一个jar文件,该文件具有我想在Rails项目中使用的类。 I have tried to import the class with Rjb with these commands. 我试图通过这些命令用Rjb导入类。 The jar file is in the bin directory. jar文件位于bin目录中。

Rjb.load("#{Rails.root}/bin")
=> nil
Rjb::add_jar("excel_tools.jar")
=> true
Rjb::import("tools.CellEditor")
ClassNotFoundException: tools.CellEditor
Rjb::import("tools/CellEditor")
ClassNotFoundException: tools.CellEditor

The class name "tools.CellEditor" should be correct. 类名“ tools.CellEditor”应该正确。 At least when I list the classes in the jar in terminal I get this and many more classes from apache poi and log4j. 至少当我在终端的jar中列出类时,我从apache poi和log4j获得了这个以及更多的类。

$ jar tvf bin/excel_tools.jar
6926 Mon Aug 25 13:24:00 EEST 2014 tools/CellEditor.class

Any idea where the jar or class loading goes wrong? 知道jar或类加载出错的地方吗?

I got this working by importing all the jar files my java class depends on. 我通过导入我的java类所依赖的所有jar文件来完成此工作。 It is not enough that the dependence jars are bundled into another jar file with my class, they need to be loaded separately before importing the class it self. 依赖项jar与我的类一起捆绑到另一个jar文件中是不够的,它们需要在导入其自身的类之前分别加载。

I copied all the jars into a java_libs directory and wrote an initializer like this 我将所有罐子复制到java_libs目录中,并编写了一个这样的初始化程序

require 'rjb'

JARS = Dir.glob("#{Rails.root}/lib/java_libs/*.jar").join(':')
Rjb::load(JARS)
CELL_EDITOR = Rjb::import('tools.CellEditor')

Now I can use the CELL_EDITOR methods anywhere in my code. 现在,我可以在代码中的任何地方使用CELL_EDITOR方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM