JRuby: import vs include vs java_import vs include_class
You can find quite a few examples about working with Java classes at:
https://github.com/jruby/jruby/wiki/CallingJavaFromJRuby
It states, that you should use java_import
instead of import
due to the JRUBY-3171 bug.
Also include_class
is or will be deprecated (JRUBY-3797) in favor of java_import
.
Currently java_import
is the recommended way to import a Java class.
Can I import Java class files into JRuby?
I've managed to answer my own question :)
If your class files are compiled to a relative path of "target", e.g. foo.Bar is located in "target/foo/Bar.class", then you do the following:
require 'java'
require 'target/foo/Bar'
module Foo
include_package 'foo'
end
puts Foo::Bar.new
And the result:
foo.Bar@1582a7c
What is the difference between include and require in Ruby?
What's the difference between
"include" and "require" in Ruby?Answer:
The include and require methods do
very different things.The require method does what include
does in most other programming
languages: run another file. It also
tracks what you've required in the
past and won't require the same file
twice. To run another file without
this added functionality, you can use
the load method.The include method takes all the
methods from another module and
includes them into the current module.
This is a language-level thing as
opposed to a file-level thing as with
require. The include method is the
primary way to "extend" classes with
other modules (usually referred to as
mix-ins). For example, if your class
defines the method "each", you can
include the mixin module Enumerable
and it can act as a collection. This
can be confusing as the include verb
is used very differently in other
languages.
Source
So if you just want to use a module, rather than extend it or do a mix-in, then you'll want to use require
.
Oddly enough, Ruby's require
is analogous to C's include
, while Ruby's include
is almost nothing like C's include
.
Importing and using a java class in ruby
I would firstly suggest packaging your classes into a jar
file in the typical java fashion. Then you can simply add require "myjar.jar"
to your JRuby script and work with your classes just as you are doing with the javax.swing
classes.
If for some reason you cannot do that, apparently you can include .class
file directly as follows:
Let's say I have 'myclass.class' which has a class called 'myclass' in
'mypackage.myclass'. This causes two problems because JRuby doesn't
like non-Camelcased classes (because Modules have to have cap-Alpha
starts I think).First, create a directory (under lib/ for example - though not
necessary) called mypackage and put myclass.class in there.Then this should work:
require 'java'
$CLASSPATH << 'lib'myclass = JavaUtilities.get_proxy_class('mypackage.myclass')
@myclass = myclass.new
How to include class dependency jar in JRuby?
My $CLASSPATH
environment variable was not set, which it needs to be in order for JRuby to be able to find my jar
. After I ran export CLASSPATH=".:mydependency.jar"
on the command line and removed the line require './mydependency.jar'
, it worked.
Related Topics
Concurrent Requests with Mri Ruby
Polymorphic Association with Multiple Associations on the Same Model
Heroku: How to Push Seeds.Rb to Existing Rails App
Dynamically Creating Class in Ruby
Ruby: "&& Return" VS "And Return"
How to Configure Mongomapper and Activerecord in Same Ruby Rails Project
Print All Method Names of a Class in Ruby
Cannot Load Such File -- 1.9/Bcrypt_Ext (Loaderror)
How to Loop Through a Daterange with Different Intervals
Truncate String to the First N Words
Refactoring a Large Routes.Rb File in Rails 4
Resetting a Singleton Instance in Ruby
Running Multiple Background Parallel Jobs with Rails
How to Run Irb.Start in Context of Current Class
Multiple Blogs in Single Jekyll Website
How to Save Dates in Local Timezone to Db with Rails3
Getting the Full Rspec Test Name from Within a Before(:Each) Block