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:
- First clone the repository to a directory of your choice.
- Next attempt to build the repository using the ant command: ant -f make/build.xml
- 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
- 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.
- 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.
Are you a Developer working with PDF files?
Our developers guide contains a large number of technical posts to help you understand the PDF file Format.
Do you need to solve any of these problems?
Display PDF documents in a Web app |
Use PDF Forms in a web browser |
Convert PDF Documents to an image |
Work with PDF Documents in Java |