8

The documentation seems to suggest that in order for me to import Java classes into JRuby, that they must be in a JAR file:

"In order to use resources within a jar file from JRuby the jar file must either be on the classpath or you can make it available with the require method" (http://wiki.jruby.org/wiki/Calling_Java_from_JRuby#Require_a_jar_file_to_make_resources_in_the_jar_discoverable_within_JRuby)

Is it at all possible to import .class files directly?

Thanks!

Roy Tinker
  • 10,044
  • 4
  • 41
  • 58
bjnortier
  • 2,008
  • 18
  • 18

2 Answers2

9

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
bjnortier
  • 2,008
  • 18
  • 18
5

I think you could also:

require 'java'
$CLASSPATH << "target"

and then

foo.bar.baz.Class.new() # ... 
# or 
java_import 'foo.bar.baz.Class'
reto
  • 16,189
  • 7
  • 53
  • 67