Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 379
max Avatar asked Nov 17 '12 04:11

max


People also ask

Which JavaScript is replaced with Nashorn?

With Java 8, Nashorn, a much improved javascript engine is introduced, to replace the existing Rhino. Nashorn provides 2 to 10 times better performance, as it directly compiles the code in memory and passes the bytecode to JVM.

Why is Nashorn deprecated?

With the release of Java 11, Nashorn was deprecated citing challenges to maintenance, and has been removed from JDK 15 onwards. Nashorn development continues on GitHub as a standalone OpenJDK project and the separate release can be used in Java project from Java 11 and up.

What is the substitute of Rhino JavaScript engine in Java 8?

Technical Article. Until Java SE 7, JDKs shipped with a JavaScript scripting engine based on Mozilla Rhino. Java SE 8 will instead ship with a new engine called Oracle Nashorn, which is based on JSR 292 and invokedynamic .

Which is New command-line tool for the Nashorn JavaScript engine in Java 8?

Executing JavaScript code by using console: For Nashorn engine, Java 8 introduced one new command-line tool i.e.jjs.


2 Answers

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.

like image 25
Lyndon Armitage Avatar answered Oct 04 '22 09:10

Lyndon Armitage


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

like image 188
pd40 Avatar answered Oct 04 '22 08:10

pd40