【发布时间】:2018-02-14 16:54:43
【问题描述】:
ScriptEngineManager manager = new ScriptEngineManager(null);
ScriptEngine engine = manager.getEngineByName("nashorn");
BundleContext context = FrameworkUtil.getBundle(FormulaImpl.class).getBundleContext();
URL configURL = context.getBundle().getEntry("eval.txt");
if (configURL != null) {
InputStream input = null;
try {
input = configURL.openStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
StringBuilder out = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
out.append(line);
}
engine.eval(out.toString());
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
} finally {
try {
input.close();
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}
}
Object obj = engine.get("formulaColumn");
在这段代码中,obj 是作为对象数组获取的。 我无法转换为对象。 在 java 1.7 中使用 Rhino 对象正在获取。 我试过了
Object obj = (Object)ScriptUtils.convert(engine.get("formulaColumn"), Object.class);
但在 java 1.8 中没有显示 calssDef 错误
我正在使用 osgi。我导出了 jdk.nashorn.api.scripting.ScriptUtils 。 从我的 jsfile 我返回一个数组。
【问题讨论】:
-
您的问题几乎无法阅读。 NoClassDefFoundError 表示您的类路径中没有必要的类,请将缺少的 jar 添加到您的构建路径中
-
您能发布完整的 stackTrace 吗?您似乎不太可能错过 ScriptUtils 类,因为它包含在 JDK 中。
-
java.lang.NoClassDefFoundError: jdk/nashorn/api/scripting/ScriptUtils 显示错误。我添加了 jdk/nashorn/api/scripting/ScriptUtils 。但运行时却无法获取。
-
在使用 Rhino 的 java 1.7 中我越来越正确。对象 obj = engine.get("formulaColumn");
-
我也在 pom.xml 的 Export 标记中添加了 jdk.nashorn.api.scripting.*
标签: java java-8 osgi apache-karaf nashorn