1

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
user1934428
  • 19,864
  • 7
  • 42
  • 87
  • On a tangent, I would strongly recommend either sticking to the 9.3 branch or waiting for JRuby 9.4.1.0 before upgrading. The 9.x.0.0 versions of JRuby are a bit notorious for having bugs, and I know there's at least one fairly serious JIT bug in 9.4.0.0 (causing some method calls to crash or misbehave once your code has been running long enough) that [has been fixed in master](https://github.com/jruby/jruby/commit/129bd6312839fed1e7054af22607bbcd09b8ec21) but not in any released version yet. – Ilmari Karonen Jan 19 '23 at 12:39
  • @IlmariKaronen : Thank you for pointing this out. I'm now doing just the first experiments with the new version. For switching from 1.7 to 9.x, I will for sure wait first for 9.4.1.0 to appear. – user1934428 Jan 20 '23 at 09:03

1 Answers1

2

On this page:

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

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

olleolleolle
  • 1,918
  • 16
  • 20
  • This seems to be the right answer. In particular, running the OP's code in verbose mode on JRuby 9.3.9.0, I get a warning saying "import is deprecated; use java_import". Presumably the deprecated ability to use `import` instead of `java_import` was removed in JRuby 9.4. – Ilmari Karonen Jan 19 '23 at 12:33