从JRuby调用Java方法抛出“没有构造函数的参数错误”

时间:2013-07-14 01:34:46

标签: java jruby constraints knapsack-problem jacop

我正在尝试从jRuby的JaCop约束编程库初始化this class。我正在使用正确类型的参数,但由于某种原因,我不断收到此错误消息:

  NameError: no constructor for arguments
    (
      org.jruby.RubyArray,
      org.jruby.RubyArray,
      org.jruby.RubyArray,
      JaCoP.core.IntVar,
      JaCoP.core.IntVar)
    on Java::JaCoPConstraintsKnapsack::Knapsack
    (root) at rb/knapsack.rb:24

它指向的代码是:

k = Jacop::Knapsack.new(@values, @weights, quantity, knapsackCapacity, knapsackProfit)

Java类构造函数的签名是:

public Knapsack(int[] profits,
                int[] weights,
                IntVar[] quantity,
                IntVar knapsackCapacity,
                IntVar knapsackProfit)

我不明白为什么jRuby抱怨找不到构造函数,因为它应该能够找到它。

1 个答案:

答案 0 :(得分:3)

JRuby不能总是猜测"复杂"方法args,你可能想尝试:

Jacop::Knapsack.new(@values, @weights, quantity.to_java(JaCoP.core.IntVar), knapsackCapacity, knapsackProfit)

甚至帮助int[]演员(不一定非必要):

Jacop::Knapsack.new(@values.to_java(:int), @weights.to_java(:int), quantity.to_java(JaCoP.core.IntVar), knapsackCapacity, knapsackProfit)