如何从jruby调用java方法

问题描述 投票:1回答:1

我正在学习Jruby,我的日食中有以下代码

public class Test {
  public Test()
  {
      System.out.print("object created");
  }
  public static void main(String args[])
  {}
}

现在我将这个项目导出为runnable jar。因为runnable jar寻找主要方法,所以我创建了主要方法。

现在我可以像这样在jruby中创建Test Class的对象

require 'java'
require 'test.jar'

foo = Test.new

它给了我输出object created

现在我想将其导出为jar而不是runnable jar。因此,对于出口项目,不需要jar main method

所以我的java代码看起来像这样

public class Test {
  public Test()
  {
    System.out.print("object created");
  }
}

然后我再次创建与上述相同的Test Class对象

require 'java'
require 'test.jar'

foo = Test.new

现在它给了我错误

NameError: uninitialized constant Test
  const_missing at org/jruby/RubyModule.java:3309
          <top> at test.rb:4

由于我是jruby的新手,我不知道为什么出口作为runnable jar工作,但出口作为jar不起作用。

哪一个是最好的方式,出口为runnable jar或出口为jar

请详细解释,因为我是jruby的新手并且提供的文档不多。

eclipse jruby
1个回答
0
投票

您需要导入该类。您已经包含了Jar包但没有导入类本身。像这个java_import "Test"导入它

© www.soinside.com 2019 - 2024. All rights reserved.