Nashorn java.lang.NoClassDefFoundError: jdk/nashorn/api/scri



我正在迁移我的Eclipse RCP以使用JDK 8,并且我大量使用JSScriptEngine。现在引入了Nashorn,我必须添加以下行以使importClassimportPackage函数工作:

load("nashorn:mozilla_compat.js");

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

我在Eclipse RCP中使用Nashorn。当我从Javascript调用Java函数并尝试使用发送的参数时,就会出现问题。我要发送的参数是一个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;
}
}

test.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;

异常消息准确地说:

jdk.nashorn.api.scripting.JSObject无法通过com.test.nashorn_1.0.0.qualifier 找到

所以。。我解决了我的问题,并能够在代码中使用JSObject。我所做的是:

  • -Dorg.osgi.framework.bundle.parent=ext添加到myproduct.product文件

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

最新更新