How to make use of Nashorn on Java 7

Nashorn is the new Oracle library to replace the reliable and now ageing, Rhino JavaScript engine for Java. It makes use of new JVM features (added specially) which should give it a large speed boost.

But like many developers we wanted to be able to evaluate it and have the added benefits of using Nashorn now without going through the hassle of building the OpenJDK8 to work with (a task that I have attempted previously).

Luckily somebody called ramonza on BitBucket has created a backport of the Nashorn repository compatible with JDK7, that is very easy to build and start playing with.

The BitBucket page can be located here: https://bitbucket.org/ramonza/nashorn-backport and to get started using it all you need to do is the following:

  1. First clone the repository to a directory of your choice.
  2. Next attempt to build the repository using the ant command: ant -f make/build.xml
    1. If the build fails due to an issue with dynalink (as mine did) somebody has graciously provided a patch file to fix this issue here. Apply it to the repository and try the build again and it should succeed
  3. Now you should see a few files in the dist folder of the cloned repository. These include the nashorn.jar as well as the javadocs for nashorn.
  4. Finally all you need to do is add the following Virtual Machine options to your Java programs to make Nashorn available to them:
 -Xbootclasspath/a:PATH/TO/nashorn-backport/dist/nashorn.jar

And that’s it! You should now be able to use Nashorn in your Java 7 programs. To test this you can run this simple Java program that will list the available Script Engines:

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();
		}
 
		ScriptEngine nashorn = manager.getEngineByName("nashorn");
		if(nashorn != null) {
			System.out.println("Nashorn is present.");
		}
		else {
			System.out.println("Nashorn is not present.");
		}
	}
 
	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());
	}
}

When run with nashorn added to the boot classpath you will see the message “Nashorn is present.” as well as some basic information on the available scripting engines. Running it without adding nashorn to the boot classpath you will see the message “Nashorn is not present.” along with basic information on only Rhino.

Hopefully this will help you to get started with using and developing applications that make use of Nashorn.

If you’re a first-time reader, or simply want to be notified when we post new articles and updates, you can keep up to date by social media (Twitter, Facebook and Google+) or the  Blog RSS.

The following two tabs change content below.
Lyndon is a Developer at IDR Solutions. He currently focuses mostly on the JavaScript in the Viewer and PDF to HTML5 Converter and also the Android PDF Viewer. He gave a short talk at the GlassFish UnConference before JavaOne 2012. Outside of IDR Solutions he has a keen interest in AI and Games Programming and runs a blog that he periodically updates.

Related Posts:

lyndon

About Lyndon Armitage

Lyndon is a Developer at IDR Solutions. He currently focuses mostly on the JavaScript in the Viewer and PDF to HTML5 Converter and also the Android PDF Viewer. He gave a short talk at the GlassFish UnConference before JavaOne 2012. Outside of IDR Solutions he has a keen interest in AI and Games Programming and runs a blog that he periodically updates.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>