3

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 ?

MN94
  • 93
  • 6
  • It looks like you are on a Unix-like operating system. On a Unix-like operating system, the path component separator is `/`, not `\\`. – Jörg W Mittag Mar 09 '17 at 17:47

1 Answers1

3

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
  • is main being called with *test.new*? I am redirecting the console output to a file (output.txt) but this file remains empty after calling *text.new*. When I execute it in the IDE I do have "Hello, World" in my output.txt file. – Karen Gonzalez Nov 23 '17 at 20:51