javaruby-on-railsrubyrjb

RJB Hello World Example


I am trying to call a function from a java class in my Ruby on Rails project using RJB (Ruby Java Bridge).

The Java class is

public class HelloWorld {

    int fifty(){
        return 50 ;
    }
   public static void main(String[] args) {
      // Prints "Hello, World" in the terminal window.
      System.out.println("Hello, World");
   }
}

and in the controller I have

  require "rjb"
  def home
      myclass = Rjb::load(classpath ='\\home\\mennatallah\\TopicalClusteringofTweets\\lib\\java_libs\\helloworld.class', jvmargs=[])

      myclass_instance = myclass.new
      @output =   myclass_instance.fifty
  end

It gives " undefined method `new' for nil:NilClass " How can I do this ?


Solution

  • you can try the following. it might help:

    Rjb::add_jar( Dir.glob("#{Rails.root}/lib/java_libs/*.jar").join(':'))
    Rjb::load(Dir.glob("#{Rails.root}/lib/java_libs/*.jar").join(':'))
    test = Rjb.import('HelloWorld')
    instance_class  = test.new