Loading jRuby-generated .class files in 9.0.0.0.rc1

I’ve generated a .class file with the following script:

require ‘jruby/core_ext’

class Example
def initialize
puts “Hello from jRuby-land!”
end

become_java! ‘.’
end

A different JVM reads the .class file into a byte array and loads it
like this:

Ruby runtime = Ruby.newInstance();
ClassReader cr = new ClassReader(bytes);
String className = cr.getClassName().replace(’/’, ‘.’);

ClassDefiningJRubyClassLoader oscl = new
ClassDefiningJRubyClassLoader(runtime.getJRubyClassLoader());
Class<?> klass = oscl.defineClass(className, klass);

I get a Java Class back, but I’m not able to instantiate it. If I call
klass.newInstance(), I get a NullPointerException. I’m sure I’ve made
this much harder than it should be, but I haven’t found any clues
digging through the jRuby project.

I feel like this is really close to working, but I don’t think I’ll get
there on my own. Any help would be greatly appreciated. Thanks!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs