java noshorn Introduction

阳光穿透心脏的1/2处 2022-11-02 11:47 59阅读 0赞

1 Introduction

This chapter provides introductory information about the Nashorn engine and how it can be used to interpret JavaScript code in a Java application or from the command line.

The Nashorn engine is an implementation of the ECMAScript Edition 5.1 Language SpecificationFoot1. It was fully developed in the Java language as part of the Nashorn projectFoot2. The code is based on the new features of the Da Vinci MachineFoot3, which is the reference implementation of Java Specification Request (JSR) 292: Supporting Dynamically Typed Languages on the Java PlatformFoot4.

The Nashorn engine is included in the Java SE Development Kit (JDK). You can invoke Nashorn from a Java application using the Java Scripting API to interpret embedded scripts, or you can pass the script to the jjs or jrunscript tool.







Note:

Nashorn is the only JavaScript engine included in the JDK. However, you can use any script engine compliant with JSR 223: Scripting for the Java PlatformFoot1, or implement your own. For more information, see Java Scripting Programmer’s Guide at http://docs.oracle.com/javase/8/docs/technotes/guides/scripting/prog_guide/index.html

Footnote1http://jcp.org/en/jsr/detail?id=223

Invoking Nashorn from Java Code

To invoke Nashorn in your Java application, create an instance of the Nashorn engine using the Java Scripting API.

To get an instance of the Nashorn engine:

  1. Import the javax.script package.

    The Java Scripting API is composed of classes and interfaces in this package. For more information about the javax.script package, see the specification at http://docs.oracle.com/javase/8/docs/api/javax/script/package-summary.html

  2. Create a ScriptEngineManager object.

    The ScriptEngineManager class is the starting point for the Java Scripting API. A ScriptEngineManager object is used to instantiate ScriptEngine objects and maintain global variable values shared by them.

  3. Get a ScriptEngine object from the manager using the getEngineByName() method.

    This method takes one String argument with the name of the script engine. To get an instance of the Nashorn engine, pass in "nashorn". Alternatively, you can use any of the following: "Nashorn", "javascript", "JavaScript", "js", "JS", "ecmascript", "ECMAScript".

After you have the Nashorn engine instance, you can use it to evaluate statements and script files, set variables, and so on. Example 1-1 provides simple Java application code that evaluates a print("Hello, World!"); statement using Nashorn.

Example 1-1 Evaluating a Script Statement Using Nashorn (EvalScript.java)

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






Note:

The eval() method throws a ScriptException that must be handled properly.

For more information about using scripts in Java code, see Java Scripting Programmer’s Guide at http://docs.oracle.com/javase/8/docs/technotes/guides/scripting/prog_guide/index.html

Invoking Nashorn from the Command Line

There are two command-line tools that can be used to invoke the Nashorn engine:

  • jrunscript

    This is a generic command that invokes any available script engine compliant with JSR 223. By default, without any options, jrunscript invokes the Nashorn engine, because it is the default script engine in the JDK.

    For more information about jrunscript, see the tool’s reference page at http://docs.oracle.com/javase/8/docs/technotes/tools/windows/jrunscript.html

  • jjs

    This is the recommended tool, created specifically for Nashorn. To evaluate a script file using Nashorn, pass the name of the script file to the jjs tool. To launch an interactive shell that interprets statements passed in using standard input, start the jjs tool without specifying any script files.

    For more information about jjs, see the tool’s reference page at http://docs.oracle.com/javase/8/docs/technotes/tools/windows/jjs.html

发表评论

表情:
评论列表 (有 0 条评论,59人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Java - JVM Introduction

    分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请点击[http://www.captainbed.net][http_www.cap

    相关 TypeScript Introduction

    今天Fred同学在组内做了一个TypeScript讲座,先把PPT截图放到这里,不知道这里能不能发视频,可以的话抽空把视频也上传一下。 接下来抽时间研究一下在Dynamic

    相关 REST Introduction

    REST(Representational State Transfer表述性状态转移)是一种针对网络应用的设计和开发方式,可以降低开发的复杂性,提高系统的可伸缩性。 RES