nashorn的{Wicket配置

时间:2016-11-22 17:18:40

标签: wicket nashorn

我最近将java版本从java 1.7升级到java 1.8。升级后,我收到此错误。

引起:ECMAScript异常:类型错误:找不到ScriptObject和My Interface的公共类加载器。

我需要使用哪个版本的wicket,它支持java 1.8和nashorn脚本引擎。我还需要为wicket配置与脚本引擎相关的任何内容。

我尝试添加此依赖项

<dependency>
            <groupId>org.wicketstuff</groupId>
            <artifactId>wicketstuff-nashorn</artifactId>
             <version>7.4.0</version>
        </dependency>

和ScriptEngineManager sem = new ScriptEngineManager();         engine = sem.getEngineByName(&#34; nashorn&#34;);

但我仍然遇到同样的问题。

请帮我解决这个问题。

以下是我的方法

private final ScriptEngine engine;
ScriptEngineManager sem = new ScriptEngineManager();
engine = sem.getEngineByName("nashorn");

public <K> K getNewInterface(MyScript myScript){
        ScriptContext ctx = new SimpleScriptContext();
        String script = myScript.getScript();
        if(Strings.isEmpty(script)) {markInvalid(myScript, "Script is empty", null); return null;}
        script += " (function(){return this;})();";
        Object thiz;
        try{
            thiz = engine.eval(script, ctx);
        } catch (ScriptException e){
            markInvalid(myScript, "Can't execute script", e);
            return null;
        }
        if(thiz==null) {markInvalid(myScript, "Script executed, but context is null", null); return null;}
        K ret = (K) ((Invocable)engine).getInterface(thiz, myScript.getScriptInterfaceClass());
        if(ret==null) {
            markInvalid(myScript, "Script executed, but it's incompatible with required interface", null);
            return null;
        }else{
            myScript.setValid(true);
            return ret;
        }
    }

1 个答案:

答案 0 :(得分:1)

Wicket不需要Nashorn。您可以将Wicket 1.5 / 6.x / 7.x / 8.x与Java 8一起使用。

wicketstuff-nashorn绝对不需要运行Wicket应用程序。

如果没有实际错误,我们很难说出它失败的原因。

更新:为什么使用new ScriptEngineManager(null),即null ClassLoader。更好地使用new ScriptEngineManager(),它将使用最有可能了解这两个类的上下文类加载器。或者使用new ScriptEngineManager(YourInterface.class.getClassLoader())