Stats

623 Contributors: 8 Thursday, January 5, 2017
Licensed under: CC-BY-SA
Not affiliated with Stack Overflow
Rip Tutorial: riptutorial@gmail.com
Roadmap: roadmap
Download eBook

Nashorn JavaScript engine

Download java eBook

Introduction

Nashorn is a JavaScript engine developed in Java by Oracle, and has been released with Java 8. Nashorn allows embedding Javascript in Java applications via JSR-223 and allows to develop standalone Javascript applications, and it provides better runtime performance and better compliance with the ECMA normalized Javascript specification.

Syntax

  • ScriptEngineManager // Provides a discovery and installation mechanism for ScriptEngine classes; uses a SPI (Service Provider Interface)
  • ScriptEngineManager.ScriptEngineManager() // Recommended constructor
  • ScriptEngine // Provides the interface to the scripting language
  • ScriptEngine ScriptEngineManager.getEngineByName(String shortName) // Factory method for the given implementation
  • Object ScriptEngine.eval(String script) // Executes the specified script
  • Object ScriptEngine.eval(Reader reader) // Loads and then executes a script from the specified source
  • ScriptContext ScriptEngine.getContext() // Returns the default bindings, readers and writers provider
  • void ScriptContext.setWriter(Writer writer) // Sets the destination to send script output to

Remarks

Nashorn is a JavaScript engine written in Java and included in Java 8. Everything you need is bundled in the javax.script package.

Note that the ScriptEngineManager provides a generic API allowing you to obtain script engines for various scripting languages (i.e. not only Nashorn, not only JavaScript).

Related Examples