【问题标题】:java.lang.NoClassDefFoundError: jdk/nashorn/api/scripting/ScriptUtils in osgi bundlejava.lang.NoClassDefFoundError:osgi 包中的 jdk/nashorn/api/scripting/ScriptUtils
【发布时间】: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


【解决方案1】:

如果您从 java 脚本返回数据,我认为您不必在 java 代码中使用 ScriptUtils.convert() 方法,如下所示:

返回 Java.to(data,"java.lang.Object")

我希望,这会有所帮助。

【讨论】:

  • 谢谢谢米姆。我在 js 文件中添加了返回类型为 Java.to(data,"java.lang.Object") 并且它正在工作。
猜你喜欢
  • 2019-01-13
  • 2021-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-22
  • 1970-01-01
  • 2016-10-29
相关资源
最近更新 更多