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

风格不统一 提交于 2019-11-30 06:13:20

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

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

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

I think it is time for a tag on SO!

Update Dec 1 2012:

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

Update Mar 10, 2013:

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

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

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.

If you checkout nashorn~jdk8 from source you can build 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:

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

or with nashorn.jar in the path:

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

Update Mar 19, 2014:

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

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

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

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

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

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

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

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.

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:

$ jjs test.js

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:

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

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.

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!