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.

Matthew Crumley
  • 101,441
  • 24
  • 103
  • 129
max
  • 2,346
  • 4
  • 26
  • 34
  • 2
    According to the roadmap it will be introduced in JDK 8 at late-2013, see http://www.oracle.com/us/corporate/press/1854982 for more details – tuxtor Nov 17 '12 at 05:07
  • 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. – max Nov 17 '12 at 20:18
  • It seems that starting with Java 11 they want to deprecate Nashhorn. See https://openjdk.java.net/jeps/335 – Alex Apr 04 '19 at 06:01

4 Answers4

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.

pd40
  • 3,187
  • 3
  • 20
  • 29
  • 1
    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. :) – max Nov 17 '12 at 14:40
  • 4
    "1.7 release 3 PRERELEASE" is Rhino, not Nashorn. – Seth Tisue Jan 16 '13 at 02:41
  • 1
    @Seth. You are right. 1.7 is not nashorn. My mistake was to compare it with JDK6. Updated answer. – pd40 Mar 10 '13 at 12:23
  • 2
    [Java 8 JDK](http://openjdk.java.net/projects/jdk8/) was [officially released on March 18, 2014](http://mreinhold.org/blog/jdk8-ga), and it includes [nashhorn](http://openjdk.java.net/jeps/174) – noe Mar 20 '14 at 00:42
  • Where can I find a good API reference for Nashorn? Much like this https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference. Is the DOM API the only thing excluded from Nashorn's implementation of ECMAScript5.1? – coderrick Aug 10 '16 at 00:52
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.

Lyndon Armitage
  • 417
  • 4
  • 11
  • I ended up using the same method to play around in JDK 7 instead of having to build the entire JDK 8. Great suggestion! – max Apr 16 '13 at 19:05
  • 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. – Lyndon Armitage Apr 18 '13 at 08:18
  • 1
    It's also worth noting that the latest build on java.net contains nashorn now http://jdk8.java.net/download.html. This backport has some issues that have already been addressed in the latest build, specifically to do with RegEx. – Lyndon Armitage Apr 18 '13 at 10:09
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
the_marcelo_r
  • 1,847
  • 22
  • 35
  • 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. – Gabor Garami Mar 23 '14 at 08:56
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/

Lyndon Armitage
  • 417
  • 4
  • 11
  • Unfortunately the development schedule was changed, however you should still be able to find Nashorn in the OpenJDK. – Lyndon Armitage Mar 19 '13 at 15:24
  • 1
    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](http://www.meetup.com/Londonjavacommunity/events/109436382/). Hopefully information from that will be posted online somewhere as they are explaining how to build Nashorn on OpenJDK8. – Lyndon Armitage Mar 19 '13 at 15:31