CHAPTER 2

image

Executing Scripts

In this chapter, you will learn:

  • How to use the eval() method of ScriptEngine to execute scripts
  • How to pass parameters from Java code to Nashorn engine
  • How to pass parameters from Nashorn engine to Java

Using the eval() Method

A ScriptEngine can execute a script in a String and a Reader. Using a Reader, you can execute a script stored on the network or in a file. One of the following versions of the eval() method of the ScriptEngine interface is used to execute a script:

  • Object eval(String script)
  • Object eval(Reader reader)
  • Object eval(String script, Bindings bindings)
  • Object eval(Reader reader, Bindings bindings) ...

Get Scripting in Java: Integrating with Groovy and JavaScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.