首页 > 解决方案 > Nashorn java.lang.NoClassDefFoundError: jdk/nashorn/api/scripting/JSObject

问题描述

我正在迁移我Eclipse RCP的使用JDK 8并且我大量使用 JS ScriptEngine。既然Nashorn介绍了,我必须添加以下行以使importClassandimportPackage函数工作:

load("nashorn:mozilla_compat.js");

这样做之后,我得到了java.lang.NoClassDefFoundError: jdk/nashorn/api/scripting/JSObject.

我在 Eclipse RCP 中使用 Nashorn。当我Java从 调用函数Javascript并尝试使用发送的参数时,就会出现问题。我要发送的参数是我想稍后在代码Javascript中执行的函数。call

我有以下代码:

TestNashorn.java

package com.test.nashorn;

import java.io.FileNotFoundException;
import java.io.FileReader;

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

import jdk.nashorn.api.scripting.JSObject;

public class TestNashorn {

    public static void main(String[] args) {
        ScriptEngine engine = (new ScriptEngineManager()).getEngineByName("js");
        try {
            engine.eval(new FileReader("C:/Users/user/workspace_nashorn/TestNashorn/src/com/test/nashorn/test.js"));
            Object o = ((Invocable)engine).invokeFunction("generate");
        } catch (ScriptException | FileNotFoundException | NoSuchMethodException e) {
            e.printStackTrace();
        }
    }

    public static int test(JSObject o1) {
        System.out.println(o1.getClass().toString());
        JSObject som = ((JSObject)o1);
        return 1;
    }
}

测试.js

load("nashorn:mozilla_compat.js");
importClass(com.test.nashorn.TestNashorn);

function generate()
{
    function asd(variablex) { print('Hello, ' + variablex); }
    var result = TestNashorn.test(asd);
}

问题出现在线路上JSObject som = ((JSObject)o1);,虽然我可以成功import jdk.nashorn.api.scripting.JSObject;

异常消息确切地说:

com.test.nashorn_1.0.0.qualifier 找不到 jdk.nashorn.api.scripting.JSObject

标签: javajava-8osgieclipse-rcpnashorn

解决方案


所以..我必须解决我的问题并能够JSObject在我的代码中使用。我所做的是以下内容:

  • 添加-Dorg.osgi.framework.bundle.parent=extmyproduct.product文件

这将它添加到.ini我的产品构建中的文件中,该文件显示了NashornAPI 中的类。


推荐阅读