[英]Calling a Custom Java Class in JRuby
I'm having trouble trying to call a custom java class with JRuby: 我在尝试使用JRuby调用自定义Java类时遇到了麻烦:
"uninitialized constant Classifier::SentimentClassifier" “未初始化的常量分类器:: SentimentClassifier”
require 'java'
require 'lib/SentimentClassifier.jar'
class Classifier
def self.classify
classifier = SentimentClassifier.new
end
end
There is a difference between a class being available to jRuby, and actively importing it into your program - see https://github.com/jruby/jruby/wiki/CallingJavaFromJRuby . 可用于jRuby的类与将其主动导入到程序之间是有区别的-请参见https://github.com/jruby/jruby/wiki/CallingJavaFromJRuby 。
require 'lib/SentimentClassifier.jar'
tells jRuby that you want to make the contents of that jar available to your program, but it doesn't import any classes itself. require'lib
require 'lib/SentimentClassifier.jar'
告诉jRuby您想使该jar的内容对您的程序可用,但它本身不会导入任何类。
It's the same in Java - adding a jar to your program's classpath is not the same as importing one of its classes (in fact it's a prerequisite - you can't import a class that isn't on the classpath). 在Java中是一样的-向程序的类路径中添加jar与导入其类之一是不同的(实际上,这是前提条件-您不能导入不在类路径中的类)。
You need to java_import
the fully-qualified name of your class: 您需要
java_import
的全名:
require 'java'
require 'lib/SentimentClassifier.jar'
java_import 'com.yourpackage.SentimentClassifier';
class Classifier
def self.classify
classifier = SentimentClassifier.new
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.