Question

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
Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top