English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java process control

Java array

Java object-oriented (I)

Java object-oriented (II)

Java object-oriented (III)

Java Exception Handling

Java List

Java Queue (queue)

Java Map collection

Java Set collection

Java input/output (I/O)

Java Reader/Writer

Java other topics

Java 8 Nashorn JavaScript

Java 8 New Features

Nashorn is a javascript engine.

Nashorn JavaScript Engine in Java 15 is no longer available.

This has already been disabled in Java 11 is marked as:

@deprecated (forRemoval = true)

From JDK 1.8 Nashorn has replaced Rhino (JDK 1.6Starting from1.7, JDK 5.1 ) as the embedded JavaScript engine of Java. Nashorn fully supports ECMAScript 292 specification and some extensions. It uses a JSR 7 The invokedynamic introduced in the new language features included in JDK

Compared with the previous Rhino implementation, this brings 2 to  10times performance improvement.

jjs

jjs is a command-line tool based on the Nashorn engine. It accepts some JavaScript source code as parameters and executes these source codes.

For example, we create a sample.js file with the following content:

print('Hello World!');

Open the console and enter the following command:

$ jjs sample.js

The output result of the above program is:

Hello World!

jjs Interactive Programming

Open the console and enter the following command:

$ jjs
jjs> print("Hello, World!")
Hello, World!
jjs> quit()
>>

Passing parameters

Open the console and enter the following command:

$ jjs -- a b c
jjs> print('Letters: ') +arguments.join(", "))
Letters: a, b, c
jjs>

Calling JavaScript in Java

Using ScriptEngineManager, JavaScript code can be executed in Java, as shown in the following example:

import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
 
public class Java8Tester {
   public static void main(String args[]){
   
      ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
      ScriptEngine nashorn = scriptEngineManager.getEngineByName("nashorn");
        
      String name = "w3codebox";
      Integer result = null;
      
      try {
         nashorn.eval("print('" + name + ")");
         result = (Integer) nashorn.eval("10 + 2");
         
      catch(ScriptException e){
         System.out.println("Execution script error: ");+ e.getMessage());
      }
      
      System.out.println(result.toString());
   }
}

Execute the above script, and the output result is:

$ javac Java8Tester.java 
$ java Java8Tester
w3codebox
12

Calling Java in JavaScript

The following example demonstrates how to reference Java classes in JavaScript:

var BigDecimal = Java.type('java.math.BigDecimal');
function calculate(amount, percentage) {
   var result = new BigDecimal(amount).multiply(
   new BigDecimal(percentage)).divide(new BigDecimal("100"), 2, BigDecimal.ROUND_HALF_EVEN);
   
   return result.toPlainString();
}
var result = calculate(568000000000000000023,13.9);
print(result);

We use the jjs command to execute the above script, and the output is as follows:

$ jjs sample.js
78952000000000002017.94

Java 8 New Features