1

I'm having trouble trying to call a custom java class with JRuby:

"uninitialized constant Classifier::SentimentClassifier"

require 'java'
require 'lib/SentimentClassifier.jar'

class Classifier 
    def self.classify
       classifier = SentimentClassifier.new
    end
end

1 Answer 1

2

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.

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.

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

You need to java_import the fully-qualified name of your class:

require 'java'
require 'lib/SentimentClassifier.jar'
java_import 'com.yourpackage.SentimentClassifier';


class Classifier 
    def self.classify
       classifier = SentimentClassifier.new
    end
end
1
  • You can also refer to the fully-qualified name if you don't want to import the Java class: com.yourpackage.SentimentClassifier.new or Java::ComYourpackage::SentimentClassifier.new
    – Shepmaster
    Dec 11, 2013 at 13:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.