This article mainly studies the related content of Java programming Nashorn, as follows.
What is Nashorn
Nashorn, pronounced "nass-horn", is the name of a tank in Germany during World War II. It is also the new generation of JavaScript engine for Java8 - replacing the old and slow Rhino, and complying with the ECMAScript-262 version 5.1 language specifications. You may want JavaScript to run in a web browser and provide various dom operations on html, but Nashorn does not support browser DOM objects. This is a point that needs attention.
I happened to write a simple example when I was learning Java 8, so I will record it here.
File Directory:
StringFunction.java source code:
public class StringFunction { /** * String intercept */ public String sub(String str, int start, int end) { return str.substring(start, end); } /** * String splicing*/ 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 script engine*/ private ScriptEngine nashorn = new ScriptEngineManager().getEngineByName("nashorn"); /** * Execute script*/ public Object execute(String script) { ScriptContext scriptContext = new SimpleScriptContext(); // Define a function named stringfunction, which actually corresponds to 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, the result is:
bcd
abcdef
Here, if NashornTest.java is rewritten as follows:
public class NashornTest { public static void main(String[] args) { // Use objects to receive the results in the script and print 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 longer returns the object because there are already objects in the script receiving the execution results of sub and append. nashorn.execute(substring); nashorn.execute(append); } }The same result will also be output.
Summarize
The above is all about the code of Java programming Nashorn instance, I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!