Proper jruby syntax for java requirement and including java classes

I cannot find the current syntax for using java classes in Jruby. This link text :

include Java include 'java' require 'java' 

As valid parameters, although he says that the last option is up to 1.0 and does not work.
Jruby Wiki , however, says to use require 'java'

And to include the classes that I saw

 MyClass = Java::some.package.MyClass include_class Java::some.package.MyClass include_class 'some.package.MyClass' java_import Java::some.package.MyClass 

Is there one preferred method?

I am currently receiving the message "override X", since I have several java classes that match my ruby ​​class. What is the best method for storing the java namespace, so I do not get them, and are there any problems (other than the obvious Java class preceding the ruby ​​class) with this override if I never use the two ruby ​​/ java classes in same file?

+4
source share
2 answers

I prefer

 require 'java' java_import java.lang.System 

I think the choice of require 'java' is not that import, but just a personal preference. JRubyWiki , on the other hand, says java_import is preferable because of this error report .

For name conflict between Java and JRuby classes. For ease of reading code or for maintenance purposes, I suggest avoiding using the same name for two different classes unless you want to reopen it.

Update : are you sure the "Redefine X ..." error is caused by the same name for your Java and JRuby class? I usually get this error because I java_import the same Java class several times (sometimes in different places, sometimes I upload the same file several times).

+1
source

Personally, I always use require 'java'

If you have namespace conflicts, I would suggest not including or importing, but specifying fully qualified class names, for example.

my_instance = Java::JavaUtil::Date.new

... where Java:: is the top-level module for all java classes, JavaUtil:: is the fully qualified name of the package for the class that I want, stuck together as appropriate, and Date is the actual name of the class.

+1
source

All Articles