📜 ⬆️ ⬇️

Introduction to Nashorn

Introduction


image Nashorn * is a JavaScript engine developed entirely in the Java programming language by Oracle. Based on Da Vinci Machine (JSR 292) and will be available as part of Java 8 (which is expected to be released in March 2014). It is worth noting that the execution of JavaScript (and support for scripts in general ) was already in Java 6, but it used the Rhino engine, also written in Java, but supported by the Mozilla Foundation .

On the list of innovations in Java 8 already written earlier . This article will provide a couple of simple examples that will give you an idea of ​​using Nashorn.


Application


Why do I need javascript in java? For example:

')

Examples of using


Preparatory stage

Install JDK 8 Early Access . Further, the text implies that the javac and java commands are executed for Java 8.

Hello, World!

 import javax.script.*; public class EvalScript { public static void main(String[] args) throws Exception { // create a script engine manager ScriptEngineManager factory = new ScriptEngineManager(); // create a Nashorn script engine ScriptEngine engine = factory.getEngineByName("nashorn"); // evaluate JavaScript statement try { engine.eval("print('Hello, World!');"); } catch (final ScriptException se) { se.printStackTrace(); } } } 

Compile the class:
 ./javac EvalScript.java 

And we execute it:
 ./java EvalScript 

We see the conclusion:
 Hello, World! 

Javascript + java

Nashorn allows you to use Java classes to create programs. Consider the following example:

MyScript.js

 var MyClass = Java.type("EvalScript.MyClass"); var my = new MyClass(); my.printMsg("Hello!"); 

EvalScript.java

 import javax.script.*; import java.io.*; public class EvalScript { public static void main(String[] args) throws Exception { // create a script engine manager ScriptEngineManager factory = new ScriptEngineManager(); // create a Nashorn script engine ScriptEngine engine = factory.getEngineByName("nashorn"); // evaluate JavaScript statement try { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); engine.eval(br); } catch (final ScriptException se) { se.printStackTrace(); } } public static class MyClass { public void printMsg(String msg) { System.out.println("printMsg : "+msg); } } } 

For example, I created my own inner class (which is not a limitation, I could have created a separate class), and called it from JavaScript code. It remains to compile the class and run it by passing our js code to the input:
 ./java EvalScript < MyScript.js 

We see the conclusion:
 printMsg : Hello! 


Conclusion


Decide for yourself how to use this feature. I became interested when I had the need to introduce automation into an existing Java project, which could be configured not by the programmer, but by the application administrator, directly through the application interface (and this would not require recompilation of the application components).

Materials used:





* Nashorn is the German word that translates into Russian as “rhino”, and into English as “rhinoceros”, which echoes Rhino , the name of the JavaScript engine implemented in Java and supported by the Mozilla Foundation. Rhino, in turn, was named after the animal depicted on the cover of a book on JavaScript published by O'Reilly Media.

Source: https://habr.com/ru/post/195870/


All Articles