Java programming Nashorn instance code, javanashorn instance
This article focuses on Nashorn programming in Java.
What is Nashorn?
Nashorn, pronounced "nass-horn", is the name of a tank in Germany during World War II, and is also the next-generation javascript Engine of Java 8-replacing the old, slow Rhino, in line with the ECMAScript-262 5.1 language specification. 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 note.
I wrote a simple example when I learned Java 8, so I will record it here.
File directory:
- StringFunction. java, string function class
- StringNashorn. java, encapsulation Script Engine
- NashornTest. java, test engine, and engine call
StringFunction. java source code:
Public class StringFunction {/*** String truncation */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 Script Engine */private ScriptEngine nashorn = new ScriptEngineManager (). getEngineByName ("nashorn");/*** execution script */public Object execute (String script) {ScriptContext scriptContext = new SimpleScriptContext (); // defines a function named stringfunction, this function 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 running result is as follows:
Bcd
Abcdef
Here, if NashornTest. java is rewritten as follows:
Public class NashornTest {public static void main (String [] args) {// receive results from objects 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 objects, because there are already objects in the script that receive the execution results of sub and append. Nashorn.exe cute (substring); nashorn.exe cute (append );}}
The same result is also output.
Summary
The above is all about the Nashorn instance code for Java programming in this article. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!