当前位置: 动力学知识库 > 问答 > 编程问答 >

java - java_import already initialized constant

问题描述:

I just started using JRuby and I create a small test file:

require 'java'

java_import java.io.File

f = File.new ARGV[0]

When I run the program like so: jruby test.rb file.txt

I get the following warning:

/Library/Frameworks/JRuby.framework/Versions/1.6.5/lib/ruby/site_ruby/shared/builtin/javasupport/core_ext/object.rb:99 warning: already initialized constant File

The class of f is in fact the java File class, but I still get the warning, any help??

I found out this is related to the following JRuby ticket by looking in object.rb:

http://jira.codehaus.org/browse/JRUBY-3453

网友答案:

Seems like a reasonable warning to me, since Ruby already has a File class (i.e. the constant "File" was already initialized to refer to the Ruby File class).

Myself, I would probably skip the import and just do

require 'java'
f = java.io.File.new ARGV[0]

which should work and would eliminate name clashes.

You can also do

require 'java'
java_file = java.io.File
f = java_file.new ARGV[0]

or

module JavaIO
   include_package "java.io"
end

f = JavaIO::File.new ARGV[0]
分享给朋友:
您可能感兴趣的文章:
随机阅读: