简体   繁体   English

为什么JRuby ScriptManager.getEngineByName返回null?

[英]Why does JRuby ScriptManager.getEngineByName returns null?

This post is related to this one : How to put properly a libgdx application inside swing application? 这篇文章与此相关: 如何将一个libgdx应用程序正确放置在swing应用程序中? but the question is different. 但问题不同。

In this post I explained that I have two JARs : WorldEditor.jar and GameEngine.jar , and I load GameEngine.jar at runtime from WorldEditor.jar . 在这篇文章中,我解释说我有两个JAR: WorldEditor.jarGameEngine.jar ,我在运行时从WorldEditor.jar加载GameEngine.jar

My problem is with one of the libraries included in GameEngine.jar , namely JRuby. 我的问题是GameEngine.jar包含的一个库,即JRuby。

When I run java -jar GameEngine.jar everything is fine, but when I launche java -jar worldEditor.jar , the instance of JRuby ScriptManager I use returns null when I call ``getEngineByName`. 当我运行java -jar GameEngine.jar一切都很好,但是当我启动java -jar worldEditor.jar ,我使用的JRuby ScriptManager实例在调用``getEngineByName`时返回null。 I just can't point ou what is the problem. 我只是不能指出你是什么问题。

By tracing the list of ScriptManagerFactories , I saw that in the good case I have [JRuby, Rhino] , and in the bad one I have only [Rhino] . 通过跟踪ScriptManagerFactories的列表,我发现在好情况下我有[JRuby, Rhino] ,在坏情况下我只有[Rhino]

Would someone have an idea of what's going on ? 有人会对发生的事情有所了解吗?

I don't have much experience with Java's ScriptEngine , but I ran into this issue while answering another question here. 我没有使用Java的ScriptEngine丰富经验,但是在这里回答另一个问题时遇到了这个问题。 I think your problem boils down to classpath order issues. 我认为您的问题归结为类路径顺序问题。

Using this code: 使用此代码:

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class Script {
    public static void main(String args[]) throws Exception {
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("jruby");
        System.out.println(engine);
    }
}

If I run it two different ways, I get two different results: 如果以两种不同的方式运行它,则会得到两种不同的结果:

$ java -cp .:jruby.jar Script
null
$ java -cp jruby.jar:. Script
org.jruby.embed.jsr223.JRubyEngine@30c01f1c

Looking into it a bit, there is a special file in the jar that registers the various scripting containers: 仔细研究一下,jar中有一个特殊文件,用于注册各种脚本容器:

The ScriptEngineManager uses the service provider mechanism described in the Jar File Specification to obtain instances of all ScriptEngineFactories available in the current ClassLoader. ScriptEngineManager使用Jar文件规范中描述的服务提供程序机制来获取当前ClassLoader中可用的所有ScriptEngineFactories的实例。

My guess is that the JVM doesn't need to load the JRuby jar in the first case, so it hasn't registered the scripting engine. 我的猜测是,在第一种情况下,JVM不需要加载JRuby jar,因此它尚未注册脚本引擎。 It would only load that jar when it cannot find some class. 它只会在找不到某个类时才加载该jar。 This means you might be able to force it to work by using some JRuby object before you ever call into the script. 这意味着您可以在调用脚本之前通过使用一些JRuby对象来强制其工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM