我如何开始使用Oracle的Nashorn JS引擎进行编码?什么时候它会取代OpenJDK中的Rhino?

时间:2021-08-08 02:43:23

I'm looking for a way to start playing around with Oracle's new Nashorn JavaScript Engine. I've DL'd the latest OpenJDK 8 (b65) and it appears that Rhino is still the only included script engine.

我正在寻找一种方法来开始使用Oracle的新Nashorn JavaScript引擎。我已经DL了最新的OpenJDK 8(b65),看起来Rhino仍然是唯一包含的脚本引擎。

Anyone know when (or in which build) Nashorn will replace Rhino in the OpenJDK? Or even better, where I can get a JDK with it included already? I know Netbeans has already written a debugger to use it, just not sure where they got the libraries/code to start writing it.

任何人都知道Nashorn何时(或在哪个版本中)将取代OpenJDK中的Rhino?或者甚至更好,我可以在哪里获得包含它的JDK?我知道Netbeans已经编写了一个调试器来使用它,只是不确定他们从哪里获得了库/代码来开始编写它。

Anyone have some links?

有人有链接吗?

Thanks.

谢谢。

4 个解决方案

#1


15  

It looks like there is no sign of Nashorn on OpenJDK yet.

看起来OpenJDK上还没有Nashorn的迹象。

The most recent comment from Jim Laskey in Oct 2012 suggests Q4 2012:

2012年10月Jim Laskey的最新评论显示2012年第四季度:

https://blogs.oracle.com/nashorn/entry/welcome_to_the_nashorn_blog#comment-1351205506968

https://blogs.oracle.com/nashorn/entry/welcome_to_the_nashorn_blog#comment-1351205506968

I think it is time for a tag on SO!

我认为是时候在SO上使用nashorn标签了!

Update Dec 1 2012:

2012年12月1日更新:

Looks like late Dec 2012 OpenJDK may have it https://blogs.oracle.com/nashorn/entry/request_for_project_nashorn_open

看起来像2012年12月下旬OpenJDK可能有它https://blogs.oracle.com/nashorn/entry/request_for_project_nashorn_open

Update Mar 10, 2013:

2013年3月10日更新:

@Seth is correct that 1.7 release 3 PRERELEASE is not Nashorn. My mistake!

@Seth是正确的1.7版本3 PRERELEASE不是Nashorn。我的错!

JDK 8 b68 includes a yet to be merged nashorn~jdk8 branch.

JDK 8 b68包含一个尚未合并的nashorn~jdk8分支。

The README for this branch says:

该分支的README说:

The Nashorn repo is in the process of being migrated to OpenJDK and as such is incomplete in several areas. The build system is not fully integrated. When complete, Nashorn will be installed in its proper location in the JRE. Once integrated, the correct version of the JDK will be wrapped around Nashorn. In the meantime, ensure you use JDK8 b68 or later.

Nashorn回购正在迁移到OpenJDK,因此在几个方面都不完整。构建系统未完全集成。完成后,Nashorn将安装在JRE的适当位置。一旦集成,JDK的正确版本将围绕Nashorn。在此期间,请确保使用JDK8 b68或更高版本。

If you checkout nashorn~jdk8 from source you can build nashorn.jar

如果从源代码中检出nashorn~jdk8,则可以构建nashorn.jar

cd nashorn~jdk8/nashorn/make
ant clean; ant

You can request the "nashorn" engine from javax.script.ScriptEngineManager in a recent jdk 1.8 build:

您可以在最近的jdk 1.8版本中从javax.script.ScriptEngineManager请求“nashorn”引擎:

jrunscript -cp ./nashorn.jar -l "nashorn" -e "println(engine.factory.getParameter(
    javax.script.ScriptEngine.ENGINE))"
> Oracle Nashorn

or with nashorn.jar in the path:

或者在路径中使用nashorn.jar:

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("nashorn");

Update Mar 19, 2014:

2014年3月19日更新:

Update from @ncasas; JDK 8 is out and Nashorn is the default JS engine.

来自@ncasas的更新; JDK 8已经发布,Nashorn是默认的JS引擎。

#2


9  

I've done some more digging around and you can get Nashorn working with JDK7 by using a backport of it located here:

我已经做了一些挖掘,你可以让Nashorn使用JDK7,它的后端位于这里:

https://bitbucket.org/ramonza/nashorn-backport

https://bitbucket.org/ramonza/nashorn-backport

Checkout that repository and attempt to build it using ant -f make/build.xml as described on the BitBucket page

检查存储库并尝试使用ant -f make / build.xml构建它,如BitBucket页面所述

Apply the patch listed in the issues section here if you get a failed build due to dynalink (I assume it will be patched into the main repository soon by the developer).

如果由于dynalink而导致构建失败,请应用此处的问题部分中列出的修补程序(我假设开发人员很快会将其修补到主存储库中)。

Upon building it you should get a nashorn.jar file inside the dist folder of your cloned repository.

在构建它时,您应该在克隆的存储库的dist文件夹中获取一个nashorn.jar文件。

Now you need to add this jar to your bootclasspath using a VM option similar to this:

现在,您需要使用类似于此的VM选项将此jar添加到bootclasspath:

-Xbootclasspath/a:C:/nashorn-backport/dist/nashorn.jar

-Xbootclasspath / A:C:/nashorn-backport/dist/nashorn.jar

And now you should be able to use nashorn. To make sure here's a quick test program I wrote that will list out the available engine factories:

现在你应该可以使用nashorn了。为了确保这是一个我写的快速测试程序,它将列出可用的引擎工厂:

import javax.script.*;

public class NashornTest {
    public static void main(String args[]) {
        ScriptEngineManager manager = new ScriptEngineManager();
        for (ScriptEngineFactory f : manager.getEngineFactories()) {
            printBasicInfo(f);
            System.out.println();
        }
    }

    public static void printBasicInfo(ScriptEngineFactory factory) {
        System.out.println("engine name=" + factory.getEngineName());
        System.out.println("engine version=" + factory.getEngineVersion());
        System.out.println("language name=" + factory.getLanguageName());
        System.out.println("extensions=" + factory.getExtensions());
        System.out.println("language version=" + factory.getLanguageVersion());
        System.out.println("names=" + factory.getNames());
        System.out.println("mime types=" + factory.getMimeTypes());
    }
}

Running that with the bootclasspath set will list Rhino and Nashorn, without it you will only see Rhino.

使用bootclasspath设置运行它将列出Rhino和Nashorn,没有它你将只看到Rhino。

#3


5  

Install the JDK8 and create an alias for your JDK's jjs (Nashorn Interpreter), e.g., if you create a file called test.js, you can run the program with:

安装JDK8并为JDK的jjs(Nashorn Interpreter)创建别名,例如,如果创建名为test.js的文件,则可以使用以下命令运行该程序:

$ jjs test.js

Mac OS = alias jjs=’/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/jre/bin/jjs’

Mac OS = alias jjs ='/ Library / Java / JavaVirtualMachines / jdk1.8.0.jdk / Contents / Home / jre / bin / jjs'

Windows = Define an environment variable called ‘JAVA8_HOME’ and point to your jdk8 folder, then you can invoke jjs by running this command:

Windows =定义一个名为“JAVA8_HOME”的环境变量并指向您的jdk8文件夹,然后您可以通过运行以下命令来调用jjs:

> “%JAVA8_HOME%\jre\bin\jjs” test.js

#4


2  

I've been looking at how to use it recently and I currently think the only way you can start using it is if you build the OpenJDK from source as it isn't in the current version from the 7th of February.

我最近一直在研究如何使用它,我目前认为你可以开始使用它的唯一方法是从源代码构建OpenJDK,因为它不是2月7日的当前版本。

I assume it will be in the developer preview version released later this week though (21/02/2013).

我认为它将在本周晚些时候发布的开发者预览版中(2013年2月21日)。

Source: http://openjdk.java.net/projects/jdk8/

资料来源:http://openjdk.java.net/projects/jdk8/

#1


15  

It looks like there is no sign of Nashorn on OpenJDK yet.

看起来OpenJDK上还没有Nashorn的迹象。

The most recent comment from Jim Laskey in Oct 2012 suggests Q4 2012:

2012年10月Jim Laskey的最新评论显示2012年第四季度:

https://blogs.oracle.com/nashorn/entry/welcome_to_the_nashorn_blog#comment-1351205506968

https://blogs.oracle.com/nashorn/entry/welcome_to_the_nashorn_blog#comment-1351205506968

I think it is time for a tag on SO!

我认为是时候在SO上使用nashorn标签了!

Update Dec 1 2012:

2012年12月1日更新:

Looks like late Dec 2012 OpenJDK may have it https://blogs.oracle.com/nashorn/entry/request_for_project_nashorn_open

看起来像2012年12月下旬OpenJDK可能有它https://blogs.oracle.com/nashorn/entry/request_for_project_nashorn_open

Update Mar 10, 2013:

2013年3月10日更新:

@Seth is correct that 1.7 release 3 PRERELEASE is not Nashorn. My mistake!

@Seth是正确的1.7版本3 PRERELEASE不是Nashorn。我的错!

JDK 8 b68 includes a yet to be merged nashorn~jdk8 branch.

JDK 8 b68包含一个尚未合并的nashorn~jdk8分支。

The README for this branch says:

该分支的README说:

The Nashorn repo is in the process of being migrated to OpenJDK and as such is incomplete in several areas. The build system is not fully integrated. When complete, Nashorn will be installed in its proper location in the JRE. Once integrated, the correct version of the JDK will be wrapped around Nashorn. In the meantime, ensure you use JDK8 b68 or later.

Nashorn回购正在迁移到OpenJDK,因此在几个方面都不完整。构建系统未完全集成。完成后,Nashorn将安装在JRE的适当位置。一旦集成,JDK的正确版本将围绕Nashorn。在此期间,请确保使用JDK8 b68或更高版本。

If you checkout nashorn~jdk8 from source you can build nashorn.jar

如果从源代码中检出nashorn~jdk8,则可以构建nashorn.jar

cd nashorn~jdk8/nashorn/make
ant clean; ant

You can request the "nashorn" engine from javax.script.ScriptEngineManager in a recent jdk 1.8 build:

您可以在最近的jdk 1.8版本中从javax.script.ScriptEngineManager请求“nashorn”引擎:

jrunscript -cp ./nashorn.jar -l "nashorn" -e "println(engine.factory.getParameter(
    javax.script.ScriptEngine.ENGINE))"
> Oracle Nashorn

or with nashorn.jar in the path:

或者在路径中使用nashorn.jar:

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("nashorn");

Update Mar 19, 2014:

2014年3月19日更新:

Update from @ncasas; JDK 8 is out and Nashorn is the default JS engine.

来自@ncasas的更新; JDK 8已经发布,Nashorn是默认的JS引擎。

#2


9  

I've done some more digging around and you can get Nashorn working with JDK7 by using a backport of it located here:

我已经做了一些挖掘,你可以让Nashorn使用JDK7,它的后端位于这里:

https://bitbucket.org/ramonza/nashorn-backport

https://bitbucket.org/ramonza/nashorn-backport

Checkout that repository and attempt to build it using ant -f make/build.xml as described on the BitBucket page

检查存储库并尝试使用ant -f make / build.xml构建它,如BitBucket页面所述

Apply the patch listed in the issues section here if you get a failed build due to dynalink (I assume it will be patched into the main repository soon by the developer).

如果由于dynalink而导致构建失败,请应用此处的问题部分中列出的修补程序(我假设开发人员很快会将其修补到主存储库中)。

Upon building it you should get a nashorn.jar file inside the dist folder of your cloned repository.

在构建它时,您应该在克隆的存储库的dist文件夹中获取一个nashorn.jar文件。

Now you need to add this jar to your bootclasspath using a VM option similar to this:

现在,您需要使用类似于此的VM选项将此jar添加到bootclasspath:

-Xbootclasspath/a:C:/nashorn-backport/dist/nashorn.jar

-Xbootclasspath / A:C:/nashorn-backport/dist/nashorn.jar

And now you should be able to use nashorn. To make sure here's a quick test program I wrote that will list out the available engine factories:

现在你应该可以使用nashorn了。为了确保这是一个我写的快速测试程序,它将列出可用的引擎工厂:

import javax.script.*;

public class NashornTest {
    public static void main(String args[]) {
        ScriptEngineManager manager = new ScriptEngineManager();
        for (ScriptEngineFactory f : manager.getEngineFactories()) {
            printBasicInfo(f);
            System.out.println();
        }
    }

    public static void printBasicInfo(ScriptEngineFactory factory) {
        System.out.println("engine name=" + factory.getEngineName());
        System.out.println("engine version=" + factory.getEngineVersion());
        System.out.println("language name=" + factory.getLanguageName());
        System.out.println("extensions=" + factory.getExtensions());
        System.out.println("language version=" + factory.getLanguageVersion());
        System.out.println("names=" + factory.getNames());
        System.out.println("mime types=" + factory.getMimeTypes());
    }
}

Running that with the bootclasspath set will list Rhino and Nashorn, without it you will only see Rhino.

使用bootclasspath设置运行它将列出Rhino和Nashorn,没有它你将只看到Rhino。

#3


5  

Install the JDK8 and create an alias for your JDK's jjs (Nashorn Interpreter), e.g., if you create a file called test.js, you can run the program with:

安装JDK8并为JDK的jjs(Nashorn Interpreter)创建别名,例如,如果创建名为test.js的文件,则可以使用以下命令运行该程序:

$ jjs test.js

Mac OS = alias jjs=’/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/jre/bin/jjs’

Mac OS = alias jjs ='/ Library / Java / JavaVirtualMachines / jdk1.8.0.jdk / Contents / Home / jre / bin / jjs'

Windows = Define an environment variable called ‘JAVA8_HOME’ and point to your jdk8 folder, then you can invoke jjs by running this command:

Windows =定义一个名为“JAVA8_HOME”的环境变量并指向您的jdk8文件夹,然后您可以通过运行以下命令来调用jjs:

> “%JAVA8_HOME%\jre\bin\jjs” test.js

#4


2  

I've been looking at how to use it recently and I currently think the only way you can start using it is if you build the OpenJDK from source as it isn't in the current version from the 7th of February.

我最近一直在研究如何使用它,我目前认为你可以开始使用它的唯一方法是从源代码构建OpenJDK,因为它不是2月7日的当前版本。

I assume it will be in the developer preview version released later this week though (21/02/2013).

我认为它将在本周晚些时候发布的开发者预览版中(2013年2月21日)。

Source: http://openjdk.java.net/projects/jdk8/

资料来源:http://openjdk.java.net/projects/jdk8/