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

4

20

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.

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.

Anyone have some links?

Thanks.

Forearm answered 17/11, 2012 at 4:49 Comment(3)
According to the roadmap it will be introduced in JDK 8 at late-2013, see oracle.com/us/corporate/press/1854982 for more detailsOrgeat
Thanks for posting the link. I knew it was going to be in JDK 8, I just figured it would be in the OpenJDK long before the release date.Forearm
It seems that starting with Java 11 they want to deprecate Nashhorn. See openjdk.java.net/jeps/335Felder
C
15

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.

Contradict answered 17/11, 2012 at 13:12 Comment(5)
Hey thanks. I hadn't seen that comment. Looks like we're just gonna have to wait...at least its just around t he corner.. I was thinking the same thing about the missing nashorn tag. :)Forearm
"1.7 release 3 PRERELEASE" is Rhino, not Nashorn.Narial
@Seth. You are right. 1.7 is not nashorn. My mistake was to compare it with JDK6. Updated answer.Contradict
Java 8 JDK was officially released on March 18, 2014, and it includes nashhornTerat
Where can I find a good API reference for Nashorn? Much like this developer.mozilla.org/en-US/docs/Web/JavaScript/Reference. Is the DOM API the only thing excluded from Nashorn's implementation of ECMAScript5.1?Roadwork
C
9

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.

Crookback answered 26/3, 2013 at 10:48 Comment(3)
I ended up using the same method to play around in JDK 7 instead of having to build the entire JDK 8. Great suggestion!Forearm
I haven't done any benchmarking comparisons between it and Rhino yet but the error messages it shows can often be different to Rhinos and help shed light on things you might miss. Also unlike Rhino it casts strings to their literal type instead of object type when reading them from a Java object. That is to say, typeof console.testingString returns string when console is a java object with a public string called testingString, whereas in Rhino that would return object. Just a neat change that removes the need to wrap calls to java strings from JavaScript in String(); when using Rhino.Crookback
It's also worth noting that the latest build on java.net contains nashorn now jdk8.java.net/download.html. This backport has some issues that have already been addressed in the latest build, specifically to do with RegEx.Crookback
S
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:

$ 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
Salesclerk answered 26/8, 2013 at 15:5 Comment(1)
And you can edit your PATH environment variable too, and add %JAVA8_HOME%\jre\bin at the end. Don't worry, if it already have %JAVA_HOME%, it takes precedence, so javac command will be executed there. Only the new commands will be executed from JAVA8_HOME.Resent
C
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.

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/

Crookback answered 18/2, 2013 at 11:32 Comment(2)
Unfortunately the development schedule was changed, however you should still be able to find Nashorn in the OpenJDK.Crookback
The London JUG in conjunction with Oracle are having a kind of HackDay on Sunday the 24th March (this Sunday at time of writing) here's a link. Hopefully information from that will be posted online somewhere as they are explaining how to build Nashorn on OpenJDK8.Crookback

© 2022 - 2024 — McMap. All rights reserved.