JRuby class loader appears to "break" when JRuby app (using JSR223) is packaged with OneJar

I’ve packaged my application in a Jar with all dependencies accessible
via the manifest, but to package the dependencies into a single jar, I
used OneJar. I understand that OneJar modifies the class loader, which
I assume is why none of the require statements work.

The application runs fine when I launch the original jar, but when I
launch the jar created with one jar, it fails at all require statements:

/some_file.rb

require ‘java’
include Java

%w’rubygems drb pstore’.each do |dep|
begin
require dep
rescue LoadError => e
$stderr.puts “[ERROR] #{e.message}”
end
end

The Java code that loads this class is the following:

// note: jruby-complete is in the load path
public class RubyHelper {
public static final ScriptEngine rubyEngine;
public static final Invocable invocable;
static {
rubyEngine = new ScriptEngineManager().getEngineByName(“ruby”);
invocable = (Invocable) rubyEngine;
}
}

// another file
InputStream fis = getClass().getResourceAsStream("/some_file.rb");
RubyHelper.rubyEngine.eval(new InputStreamReader(fis));

Is there any fix for this? Anyway I can restore the class loader
without stomping on what OneJar does?

Hi,

On Thu, Dec 30, 2010 at 6:11 PM, Josh – [email protected] wrote:

include Java

// another file
InputStream fis = getClass().getResourceAsStream(“/some_file.rb”);
RubyHelper.rubyEngine.eval(new InputStreamReader(fis));

Is there any fix for this? Anyway I can restore the class loader
without stomping on what OneJar does?

Did you tried “java -jar somthing.jar”?
In general, when JRuby’s home directory is not correctly set,
“require” fails. Your jar doesn’t include jruby-complete.jar, right?
Then, would you try to run your jar using jruby in a basic way. I
mean, set JRUBY_HOME environment variable correctly, have jruby.jar in
your CLASSPATH environment variable, then try “java -jar …”
If this fails, it might be a classloader issue. I recently fixed this
problem in JRuby 1.5 branch, so next 1.5 release might work. If you
have a chance, would you try the code using JRuby’s
ScriptingContainer. It has a method to set a classloader. You can
figure out what the real problem is.

-Yoko