Lyndon Armitage Lyndon is a general Developer. He has a keen interest in AI and Games Programming and runs a blog that he periodically updates.

How to make use of Nashorn on Java 7

1 min read

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.

Did you know...

IDRsolutions offers a whole range of online file converters to convert PDF and Microsoft Excel, Word and Office Documents to HTML5, SVG or image formats?

It is free to use for single file conversions and also includes Developer links if you want to use our commercial software for bulk conversions. Find out more on this page

Lyndon Armitage Lyndon is a general Developer. 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 *

IDRsolutions Ltd 2020. All rights reserved.