Java教程

Java Nashorn

Nashorn是JavaScript引擎。它用于在JVM(Java虚拟机)上动态执行JavaScript代码。 Java提供了用于执行JavaScript代码的命令行工具jjs。
您可以通过使用jjs命令行工具并将其嵌入Java源代码中来执行JavaScript代码。

示例: 使用终端执行

以下是在JVM上执行JavaScript代码的分步过程。
1)创建一个文件hello.js。
2)编写以下代码并将其保存到文件中。
    var hello = function(){
        print("Hello Nashorn");
    };
    hello();
3)打开终端
4)编写命令 jjs hello.js ,然后按Enter。
执行命令后,您将看到以下内容输出。
输出:
Hello Nashorn

示例: 以Java代码执行JavaScript文件

您可以直接从Java文件中执行JavaScript文件。在以下代码中,我们将在FileReader类的帮助下读取文件hello.js。
import javax.script.*;
import java.io.*;
public class NashornExample {
    public static void main(String[] args) throws Exception{
        // Creating script engine
        ScriptEngine ee = new ScriptEngineManager().getEngineByName("Nashorn");
        // Reading Nashorn file
        ee.eval(new FileReader("js/hello.js"));
    }
}
输出:
Hello Nashorn

示例: 将JavaScript代码嵌入Java源文件中

您可以将JavaScript代码嵌入Java源文件中。 Java编译器不会抱怨,但是当您拥有大量源代码时,这不是一个好习惯。在下面的示例中,我们正在评估JavaScript代码。
import javax.script.*;
public class NashornExample {
    public static void main(String[] args) throws Exception{
        // Creating script engine
        ScriptEngine ee = new ScriptEngineManager().getEngineByName("Nashorn");
        // Evaluating Nashorn code
        ee.eval("print('Hello Nashorn');");
    }
}
输出:
Hello Nashorn

示例: 嵌入JavaScript表达式

您可以在JavaScript代码中嵌入JavaScript表达式和变量。在以下代码中,我们将变量嵌入到字符串中。要执行此程序,您需要在命令行中传递标记-scripting。
文件: hello.js
var hello = function(msg){
    print("Hello ${msg}");
};
hello("Nashron");
命令: jjs-脚本hello.js
输出:
Hello Nashorn

此处的文档

在Nashorn中,heredocs只是多行字符串。您可以使用< <后面加上特殊的终止标记eof来创建它。您还可以将javascript表达式嵌入$ {...}表达式中。< div>

示例: JavaScript文件中的Heredocs

文件: hello.js
var message = <<EOF
this is a java script file
it contains multiple lines
of code.
let's execute.
EOF
print(message)
命令: jjs-脚本hello.js
输出:
this is a java script file
it contains multiple lines
of code.
let's execute.

示例: 在Java文件中设置JavaScript变量

您可以将值传递给Java文件中的JavaScript变量。在下面的示例中,我们将变量绑定并传递到JavaScript文件。
文件: hello.js
print("Hello "+name);
文件: NashornExample.java
import javax.script.*;
import java.io.*;
public class NashornExample {
    public static void main(String[] args) throws Exception{
        // Creating script engine
        ScriptEngine ee = new ScriptEngineManager().getEngineByName("Nashorn");
        //Binding script and Define scope of script
        Bindings bind = ee.getBindings(ScriptContext.ENGINE_SCOPE);
        bind.put("name", "Nashorn");
        // Reading Nashorn file
        ee.eval(new FileReader("js/hello.js"));
    }
}
输出:
Hello Nashorn

在JavaScript文件中导入Java包

Java提供了一种在JavaScript代码内部导入Java包的功能。在这里,我们使用两种方法来导入Java包。

示例1: 在JavaScript文件中导入Java包

文件: hello.js
print(java.lang.Math.sqrt(4));
输出:
2

示例2: 在JavaScript文件中导入Java包

文件: hello.js
var importFile = new JavaImporter(java.util);
var a = new importFile.ArrayList();
a.add(12);
a.add(20);
print(a);
print(a.getClass());
输出:
[12, 20]
class java.util.ArrayList

示例3: 在JavaScript文件中导入Java包

您可以同时导入多个包。
文件: 您好。 js
var importIt = new JavaImporter(java.lang.String,java.util,java.io);
with (importIt) {
  var linkedHS = new LinkedHashSet();
  linkedHS.add(new File("abc"));
  linkedHS.add(new File("hello.js"));
  linkedHS.add("india".toUpperCase());
}
print(linkedHS);
输出:
[abc, hello.js, INDIA]

在Java代码中调用JavaScript函数

您可以在Java文件中调用JavaScript函数。在下面的示例中,我们将调用JavaScript函数。

示例: 在Java代码内部调用函数

文件: hello.js
var functionDemo1 = function(){
    print("this is JavaScript function");
}
var functionDemo2 = function(message){
    print("Hello "+message);
}
文件: NashornExample.java
import javax.script.*;
import java.io.*;
public class NashornExample {
    public static void main(String[] args) throws Exception{
        // Creating script engine
        ScriptEngine ee = new ScriptEngineManager().getEngineByName("Nashorn");
        // Reading Nashorn file
        ee.eval(new FileReader("js/hello.js"));
        Invocable invocable = (Invocable)ee;
        // calling a function
        invocable.invokeFunction("functionDemo1");
        // calling a function and passing variable as well.
        invocable.invokeFunction("functionDemo2","Nashorn");  
    }
}
输出:
this is JavaScript function
Hello Nashorn
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4