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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13427793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:40:27  来源:igfitidea点击:

How can I start coding with Oracle's Nashorn JS Engine and when will it replace Rhino in the OpenJDK?

javarhinoopenjdknashorn

提问by max

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 引擎。我已经对最新的 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.

谢谢。

采纳答案by pd40

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:

Jim Laskey 在 2012 年 10 月的最新评论表明 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 nashorntag 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 PRERELEASEis not Nashorn. My mistake!

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

JDK 8 b68includes a yet to be merged nashorn~jdk8branch.

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

The README for this branch says:

这个分支的自述文件说:

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~jdk8from 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.ScriptEngineManagerin 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.jarin 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 outand Nashorn is the default JS engine.

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

回答by Lyndon Armitage

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.xmlas described on the BitBucket page

签出该存储库并尝试使用ant -f make/build.xmlBitBucket 页面上的描述构建它

Apply the patch listed in the issues section hereif 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 添加到您的引导类路径:

-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.

使用引导类路径集运行它会列出 Rhino 和 Nashorn,没有它你只会看到 Rhino。

回答by the_marcelo_r

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 解释器)创建一个别名,例如,如果您创建一个名为 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

回答by Lyndon Armitage

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).

我认为它会出现在本周晚些时候发布的开发者预览版中(21/02/2013)。

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

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