简体   繁体   English

从JRuby调用Java方法会引发“无参数错误构造函数”

[英]Calling Java method from JRuby throws “no constructor for arguments error”

I'm trying to initialize this class from the JaCop constraint programming library from jRuby. 我正在尝试从jRuby的JaCop约束编程库初始化此类 I am using the correct type of arguments, but for some reason I keep getting this error message: 我使用的参数类型正确,但是由于某些原因,我不断收到此错误消息:

  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

The code that it points to is this: 它指向的代码是这样的:

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

The signature of the Java class constructor is this: Java类构造函数的签名是这样的:

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

I don't understand why jRuby complains that constructor is not found because it should be able to find this. 我不明白为什么jRuby抱怨找不到构造函数,因为它应该能够找到它。

JRuby can not always guess "complicated" method args for you, you might want to try : JRuby不能总是为您猜测“复杂的”方法args,您可能需要尝试:

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

or even helping with the int[] cast (should not be necessary) : 甚至没有帮助int[]强制转换(应该没有必要):

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

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

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