简体   繁体   English

RJB Hello World示例

[英]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). 我试图使用RJB(Ruby Java Bridge)在Ruby on Rails项目中从java类调用函数。

The Java class is Java类是

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 ? 它为nil提供了“未定义的方法`new”:NilClass“我该怎么做?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM