jrubyjruby-java-interop

jruby does not understand import statement


I'm converting a project written with JRuby 1.7 in mind to JRuby 9.4. One of the files starts like this:

if RUBY_PLATFORM == 'java'

  require 'java'
  import java.lang.management.ManagementFactory

  ....

end

The import does not work anymore. With JRuby 9.4, just compiling this file produces the error message NoMethodError: undefined method `import' for main:Object.

My understanding was that require 'java' would provide the import method, and at least it was working with JRuby 1.7.

In case it matters: We are using the imported class like this:

current_heap_in_bytes = ManagementFactory.getMemoryMXBean.getHeapMemoryUsage.used

Solution

  • On this page:

    https://github.com/jruby/jruby/wiki/CallingJavaFromJRuby#from-class-files

    There is mention of the java_import method. Does that help?