Java programming Nashorn instance code

  • 2021-01-11 01:59:33
  • OfStack

This paper mainly studies Java programming Nashorn related content, as follows.

What is Nashorn

Nashorn, pronounced "nass-horn", is the name of a German World War II tank and is the javascript engine for the new generation of the java8 -- replacing the old, slow Rhino, in line with the language specification ECMAScript-262 version 5.1. You might think that javascript is running in the web browser, providing various dom operations on html, but Nashorn does not support objects in the browser DOM. That's the one thing to notice.

I happened to write a simple example when I was learning Java8, so I put it here to record 1.

File directory:

StringFunction.java, String function class StringNashorn.java, encapsulated script engine ES32en. ES33en, test engine, engine call

StringFunction. java source code:


public class StringFunction { 
   
  /** 
   *  String interception  
   */ 
  public String sub(String str, int start, int end) { 
    return str.substring(start, end); 
  } 
   
  /** 
   *  String concatenation  
   */ 
  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 source code:


public class StringNashorn { 
   
  /** 
   * Nashorn The script engine  
   */ 
  private ScriptEngine nashorn = new ScriptEngineManager().getEngineByName("nashorn"); 
   
  /** 
   *  Execute the script  
   */ 
  public Object execute(String script) { 
    ScriptContext scriptContext = new SimpleScriptContext(); 
    //  define 1 called stringfunction And this function actually corresponds to 1 a StringFunction object  
    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 source code:


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()); 
  } 
} 

Run the main method, and the result is:

[

bcd
abcdef

]

Here if NashornTest java rewritten as follows:


public class NashornTest { 
   
  public static void main(String[] args) { 
    //  An object is used within the script to receive the result and print it  
    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(); 
    //  Here, execute No more objects are returned because there are already object receivers in the script sub and append The result of the execution of.  
    nashorn.execute(substring); 
    nashorn.execute(append); 
  } 
} 

It will also output the same result.

conclusion

The above is this article about Java programming Nashorn example code of all content, I hope to help you. Interested friends can continue to refer to the site of other related topics, if there are shortcomings, welcome to leave a message to point out. Thank you for your support!


Related articles: