为什么我从一个java类中获取一个NoMethodError来调用java接口的jRuby实现?

时间:2016-03-24 20:34:58

标签: java jruby

我已经按照类似问题的建议(链接:How to get from JRuby a correctly typed ruby implementation of a Java interface?),但这并没有改变症状。

Java运行时异常:

org.jruby.exceptions.RaiseException: (NoMethodError) undefined method `doWork' for main:Object

Java执行代码:

    Object worker= engine.get("worker");
    MyInterface interfacedWorker = ((Invocable) engine).getInterface(worker,
            MyInterface.class);
    interfacedWorker.doWork(1,2);

java接口:

public interface MyInterface
{
    public void doWork (int a, int b)
}

jRuby课程:

class Worker_31415_2
  java_implements 'com.myApp.MyInterface'
  def doWork(a, b)
    p a, b
  end
end
worker=Worker_31415_2.new

1 个答案:

答案 0 :(得分:0)

你应该使用这个表格: -

require 'java'
require_relative 'classes'

class Worker
  include com.myApp.MyInterface

  java_signature 'doWork(int a, int b)'    
  def doWork(a, b)
    p a, b
  end
end

Worker.new.doWork(1, 2)

根据你的例子编译的java接口的Classes.jar,对我有用!