2017-09-06 77 views
2
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"); 

在此代碼ScriptUtils,OBJ越來越作爲對象陣列。 我無法轉換成對象。 在java 1.7中使用Rhino對象越來越多。 我試着用java.lang.NoClassDefFoundError:JDK /犀牛/ API /腳本/在OSGi包

Object obj = (Object)ScriptUtils.convert(engine.get("formulaColumn"), Object.class); 

但在Java 1.8顯示

我使用OSGi沒有calssDef錯誤。我導出了jdk.nashorn.api.scripting.ScriptUtils。 從我的jsfile我返回一個數組。

+2

你的問題幾乎不可讀。 NoClassDefFoundError表示在類路徑中沒有必要的類,將缺少的jar添加到構建路徑中 –

+1

可以發佈完整的stackTrace嗎?由於它包含在JDK中,因此您不太可能缺少ScriptUtils類。 – Aaron

+0

java.lang.NoClassDefFoundError:jdk/nashorn/api/scripting/ScriptUtils 錯誤顯示。 我添加了jdk/nashorn/api/scripting/ScriptUtils。但運行時它不能得到。 – Diljish

回答

2

我覺得你沒有用在Java代碼中ScriptUtils.convert()方法,如果你是從Java腳本返回數據如下:

回報Java.to(數據,「爪哇。 lang.Object「)

我希望這會有所幫助。

+0

謝謝Shemeem。 我在js文件中將返回類型添加爲Java.to(data,「java.lang.Object」)並且它正在工作。 – Diljish