org.jruby.embed.EvalFailedException: (LoadError) n

2019-08-28 20:38发布

问题:

I've created jar file from ruby code using warbler. File structure within jar is as below:

core.jar
----core
--------lib
------------gui.class
------------gui.rb

I've other java application from which I want to run "connect" function which is in Gui class of gui.rb.

I've tried below code in java:

ScriptingContainer SCRIPTING_CONTAINER = new ScriptingContainer;
// String CORE_JAR_PATH = "core.jar";
String CORE_JAR_PATH = "core.jar!/core/lib";
SCRIPTING_CONTAINER.getLoadPaths().add(CORE_JAR_PATH);
SCRIPTING_CONTAINER.runScriptlet("require 'gui'");

The last line in above code is giving me the exception "LoadError: no such file to load -- gui". How can I call the function of gui.rb?

回答1:

core/lib/gui.rb

puts "core was loaded!"

I packed the Ruby code (not using Warbler or anything like that) as jar -cf core.jar core.

Main.java

import org.jruby.embed.ScriptingContainer;

public class Main {
    public static void main(String[] args) {
        ScriptingContainer container = new ScriptingContainer();
        container.runScriptlet("require './core.jar'");
        container.runScriptlet("require 'core/lib/gui'");
    }
}

Compiled as javac -cp jruby-1.7.9.jar Main.java -d build

Running

I was then able to run this code as java -cp jruby-1.7.9.jar:build/ Main.