Java編程Nashorn執行個體代碼,javanashorn執行個體

來源:互聯網
上載者:User

Java編程Nashorn執行個體代碼,javanashorn執行個體

本文研究的主要是Java編程Nashorn的相關內容,具體如下。

Nashorn是什麼

Nashorn,發音“nass-horn”,是德國二戰時一個坦克的命名,同時也是java8新一代的javascript引擎--替代老舊,緩慢的Rhino,符合 ECMAScript-262 5.1 版語言規範。你可能想javascript是運行在web瀏覽器,提供對html各種dom操作,但是Nashorn不支援瀏覽器DOM的對象。這個需要注意的一個點。

之前學習Java8的時候恰好寫了個簡單的例子,所以放在這裡記錄一下。

檔案目錄:

  • StringFunction.java,字串功能類
  • StringNashorn.java,封裝指令碼引擎
  • NashornTest.java,測試引擎,引擎調用

StringFunction.java源碼:

public class StringFunction {      /**    * 字串截取    */   public String sub(String str, int start, int end) {     return str.substring(start, end);   }      /**    * 字串拼接    */   public String append(String... strs) {     StringBuilder result = new StringBuilder(strs[0]);     Stream.of(strs).skip(1).forEach(str -> result.append(str));     return result.toString();   } } 

StringNashorn.java源碼:

public class StringNashorn {      /**    * Nashorn指令碼引擎    */   private ScriptEngine nashorn = new ScriptEngineManager().getEngineByName("nashorn");      /**    * 執行指令碼    */   public Object execute(String script) {     ScriptContext scriptContext = new SimpleScriptContext();     // 定義一個名為stringfunction的函數,這個函數實際對應著一個StringFunction對象     scriptContext.setAttribute("stringfunction", new StringFunction(), 100);     nashorn.setContext(scriptContext);          Object result = null;     try {       result = nashorn.eval(script);     } catch (ScriptException e) {       e.printStackTrace();     }          return result;   } } 

NashornTest.java源碼:

public class NashornTest {      public static void main(String[] args) {     String substring = "stringfunction.sub(\"abcdefghijk\", 1, 4);";     String append = "stringfunction.append(\"abc\", \"def\");";          StringNashorn nashorn = new StringNashorn();     Object subResult = nashorn.execute(substring);     Object appendResult = nashorn.execute(append);     System.out.println(subResult.toString());     System.out.println(appendResult.toString());   } } 

運行main方法,運行結果:

bcd
abcdef

這裡如果NashornTest.java改寫如下:

public class NashornTest {      public static void main(String[] args) {     // 指令碼內用對象接收結果並列印     String substring = "var s1 = stringfunction.sub(\"abcdefghijk\", 1, 4);"         + " print(s1);";     String append = "var s2 = stringfunction.append(\"abc\", \"def\");"         + " print(s2);";          StringNashorn nashorn = new StringNashorn();     // 這裡execute不再返回對象,因為在指令碼裡面已經有對象接收sub和append的執行結果。     nashorn.execute(substring);     nashorn.execute(append);   } } 

同樣也會輸出相同的結果。

總結

以上就是本文關於Java編程Nashorn執行個體代碼的全部內容,希望對大家有所協助。感興趣的朋友可以繼續參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.