java - Nashorn java.lang.NoClassDefFoundError: jdk/nashorn/api/scripting/JSObject
问题描述
我正在迁移我Eclipse RCP
的使用JDK 8
并且我大量使用 JS ScriptEngine
。既然Nashorn
介绍了,我必须添加以下行以使importClass
andimportPackage
函数工作:
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
解决方案
所以..我必须解决我的问题并能够JSObject
在我的代码中使用。我所做的是以下内容:
- 添加
-Dorg.osgi.framework.bundle.parent=ext
到myproduct.product
文件
这将它添加到.ini
我的产品构建中的文件中,该文件显示了Nashorn
API 中的类。
推荐阅读
- tornadofx - Tornadofx Tableview 行数
- sql - WHERE 子句中的 SQL IF 语句
- spring - Spring stomp over websocket SubscribeMapping 不起作用
- react-native - 带有反应原生导航的优步应用导航
- c# - 以 c# 形式创建 Windows 10 透明效果
- google-app-engine - 如何使用 gcloud 推送最新版本的 Web 应用程序以使其上线
- javascript - 从页面源代码中提取特定变量的内容
- go - netlink 序列号乱序
- javascript - Fabrics - 如何在鼠标悬停之前呈现免费的绘图内容
- nosql - Arangodb 远程复制