jruby无法加载Java类com.example.CallMe

时间:2014-10-16 08:15:27

标签: jruby

我是jruby的新手。我试图在他们的github wiki上运行示例程序。 https://github.com/jruby/jruby/wiki/JRubyAndJavaCodeExamples#JRuby_calling_Java

我的目录结构是:

D:\learnJruby\CallJava.rb
D:\learnJruby\com\example\CallMe.java
D:\learnJruby\com\example\ISpeaker.java

当我跑步时

jruby CallJava.rb

我收到此错误

NameError: cannot link Java class com.example.CallMe, probable missing dependency: com/example/CallMe (wrong name: CallMe)
         for_name at org/jruby/javasupport/JavaClass.java:1286
  get_proxy_class at org/jruby/javasupport/JavaUtilities.java:34
      java_import at file:/C:/jruby-1.7.16/lib/jruby.jar!/jruby/java/core_ext/object.rb:26
              map at org/jruby/RubyArray.java:2412
      java_import at file:/C:/jruby-1.7.16/lib/jruby.jar!/jruby/java/core_ext/object.rb:22
           (root) at CallJava.rb:4

1 个答案:

答案 0 :(得分:1)

Java正在编译语言,然后您需要在运行之前将.java文件(源代码)编译为.class文件(JVM字节码)。

运行Java时,它会在目录中查找.class文件,在类路径中查找.jars文件。

您可以从JRuby明确要求类:

  • require 'some.jar'

  • $CLASSPATH << "target/classes"; java_import org.asdf.ClassName

https://github.com/jruby/jruby/wiki/CallingJavaFromJRuby#accessing-and-importing-java-classes

还有一个建议 - 不要在类或模块之外使用java_import,因为java类将被导入到全局范围(Object类):

require 'java'
require 'foo.jar'

java_import 'foo.bar.Foo' # Bad! Foo is global constant now

module Bar
  java_import 'foo.bar.Baz' # good, Baz is constant in module Bar, e.g. Bar::Baz
end