Migration Guide from Nashorn to GraalVM JavaScript

This guide serves as a migration guide for code previously targeted to the Nashorn engine. See the Java Interoperability guide for an overview of supported Java interoperability features.

The Nashorn engine has been deprecated in JDK 11 as part of JEP 335 and and has been removed from JDK15 as part of JEP 372.

GraalVM can step in as a replacement for JavaScript code previously executed on the Nashorn engine. GraalVM provides all the features for JavaScript previously provided by Nashorn. Many are available by default, some are behind flags, and others require minor modifications to your source code.

Both Nashorn and GraalVM JavaScript support a similar set of syntax and semantics for Java interoperability. One notable difference is that GraalVM JavaScript takes a secure by default approach, meaning some features need to be explicitly enabled that were available by default on Nashorn. The most important differences relevant for migration are listed here.

Nashorn features available by default (dependent on security settings):

Nashorn Compatibility Mode

GraalVM JavaScript provides a Nashorn compatibility mode. Some of the functionality necessary for Nashorn compatibility is only available when the js.nashorn-compat option is enabled. This is the case for Nashorn-specific extensions that GraalVM JavaScript does not want to expose by default.

Note that you have to enable experimental options to use this flag. Further note that setting this flag defeats the secure by default approach of GraalVM JavaScript in some cases, e.g., when operating on a legacy ScriptEngine.

The js.nashorn-compat option can be set: 1. by using a command line option:

js --experimental-options --js.nashorn-compat=true

2. by using the Polyglot API:

import org.graalvm.polyglot.Context;

try (Context context = Context.newBuilder().allowExperimentalOptions(true).option("js.nashorn-compat", "true").build()) {
    context.eval("js", "print(__LINE__)");

3. by using a system property when starting a Java application (remember to enable allowExperimentalOptions on the Context.Builder in your application as well):

java -Dpolyglot.js.nashorn-compat=true MyApplication

Functionality only available under this flag includes:

Nashorn Syntax Extensions

Nashorn syntax extensions can be enabled using the js.syntax-extensions experimental option. They are also enabled by default in the Nashorn compatibility mode (js.nashorn-compat).

GraalVM JavaScript vs Nashorn

GraalVM JavaScript differs from Nashorn in some aspects that were intentional design decisions.

Secure by Default

GraalVM JavaScript takes a secure by default approach. Unless explicitly permitted by the embedder, JavaScript code cannot access Java classes or access the file system, among other restrictions. Several features of GraalVM JavaScript, including Nashorn compatibility features, are only available when the relevant security settings are permissive enough. Make sure you understand the security implications of any change that lifts the secure default limits to your application and the host system.

For a full list of available settings, see Context.Builder. Those flags can be defined when building the context with GraalVM Polyglot API.

Flags frequently required to enable features of GraalVM JavaScript are:

If you run code on the legacy ScriptEngine, see Setting options via Bindings regarding how to set them there.

Finally, note that the nashorn-compat mode enables the relevant flags when executing code on the ScriptEngine (but not on Context), to provide better compatibilty with Nashorn in that setup.

Launcher Name js

GraalVM JavaScript comes with a binary launcher named js. Note that, depending on the build setup, GraalVM might still ship Nashorn and its jjs launcher.

ScriptEngine Name graal.js

GraalVM JavaScript is shipped with support for ScriptEngine. It registers under several names, including “graal.js”, “JavaScript”, and “js”. Be sure to activate the Nashorn compatibility mode as described above if you need full Nashorn compatibility. Depending on the build setup, GraalVM might still ship Nashorn and provide it via ScriptEngine. For more details, see ScriptEngine Implementation.


GraalVM JavaScript supports a class filter when starting with a polyglot Context. See Context.Builder.hostClassFilter.

Fully Qualified Names

GraalVM Javascript requires the use of Java.type(typename). It does not support accessing classes just by their fully qualified class name by default. Java.type brings more clarity and avoids the accidental use of Java classes in JavaScript code. For instance, look at this pattern:

var bd = new java.math.BigDecimal('10');

It should be expressed as:

var BigDecimal = Java.type('java.math.BigDecimal');
var bd = new BigDecimal('10');

Lossy Conversion

GraalVM JavaScript does not allow lossy conversions of arguments when calling Java methods. This could lead to bugs with numeric values that are hard to detect.

GraalVM JavaScript will always select the overloaded method with the narrowest possible argument types that can be converted to without loss. If no such overloaded method is available, GraalVM JavaScript throws a TypeError instead of lossy conversion. In general, this affects which overloaded method is executed.

Custom targetTypeMappings can be used to customize behaviour. See HostAccess.Builder#targetTypeMapping.

ScriptObjectMirror Objects

GraalVM JavaScript does not provide objects of the class ScriptObjectMirror. Instead, JavaScript objects are exposed to Java code as objects implementing Java’s Map interface.

Code referencing ScriptObjectMirror instances can be rewritten by changing the type to either an interface (Map or List) or the polyglot Value class which provides similar capabilities.


Running JavaScript on GraalVM supports multithreading by creating several Context objects from Java code. Contexts can be shared between threads, but each context must be accessed by a single thread at a time. Multiple JavaScript engines can be created from a Java application, and can be safely executed in parallel on multiple threads:

Context polyglot = Context.create();
Value array = polyglot.eval("js", "[1,2,42,4]");

GraalVM JavaScript does not allow the creation of threads from JavaScript applications with access to the current Context. Moreover, GraalVM JavaScript does not allow concurrent threads to access the same Context at the same time. This could lead to unmanagable synchronization problems like data races in a language that is not prepared for multithreading. For example:

new Thread(function() {
    print('printed from another thread'); // throws Exception due to potential synchronization problems

JavaScript code can create and start threads with Runnables implemented in Java. The child thread may not access the Context of the parent thread or of any other polyglot thread. In case of violations, an IllegalStateException will be thrown. A child thread may create a new Context instance, though.

new Thread(aJavaRunnable).start(); // allowed on GraalVM JavaScript

With proper synchronization in place, multiple contexts can be shared between different threads. The example Java applications using GraalVM JavaScript Contexts from multiple threads can be found here.

Extensions Only Available in Nashorn Compatibility Mode

The following extensions to JavaScript available in Nashorn are deactivated in GraalVM JavaScript by default. They are provided in GraalVM’s Nashorn compatibility mode. It is highly recommended not to implement new applications based on those features, but only to use it as a means to migrate existing applications to GraalVM.

String length Property

GraalVM JavaScript does not treat the length property of a String specially. The canonical way of accessing the String length is reading the length property:


Nashorn allows users to access length as both a property and a function. Existing function calls length() should be expressed as property access. Nashorn behavior is mimicked in the Nashorn compatibility mode.

Java Packages in the JavaScript Global Object

GraalVM JavaScript requires the use of Java.type instead of fully qualified names. In the Nashorn compatibility mode, the following Java packages are added to the JavaScript global object: java, javafx, javax, com, org, and edu.


The JavaImporter feature is available only in the Nashorn compatibility mode.


The use of the non-standard JSAdapter feature is discouraged and should be replaced with the equivalent standard Proxy feature. For compatibility, JSAdapter is still available in the Nashorn compatibility mode.

Java.* Methods

Several methods provided by Nashorn on the Java global object are available only in the Nashorn compatibility mode, or currently not supported by GraalVM JavaScript. Available in the Nashorn compatibility mode are: Java.isJavaFunction, Java.isJavaMethod, Java.isScriptObject, and Java.isScriptFunction. Java.asJSONCompatible is currently not supported.


In the Nashorn compatibility mode, GraalVM JavaScript allows users to access getters and setters just by using the names as properties, while omitting get, set, or is:

var Date = Java.type('java.util.Date');
var date = new Date();

var myYear = date.year; // calls date.getYear()
date.year = myYear + 1; // calls date.setYear(myYear + 1);

GraalVM JavaScript mimics the behavior of Nashorn regarding the ordering of the access:

Note that Nashorn (and thus, GraalVM JavaScript) makes a clear distinction between property read/writes and function calls. When the Java class has both a field and a method of the same name publicly available, obj.property will always read the field (or the getter as discussed above), while obj.property() will always call the respective method.

Additional Aspects to Consider

Features of GraalVM JavaScript

GraalVM JavaScript supports features of the newest ECMAScript specification and some extensions to it. See JavaScript Compatibility. Note that this example adds objects to the global scope that might interfere with existing source code unaware of those extensions.

Console Output

GraalVM JavaScript provides a print builtin function compatible with Nashorn.

Note that GraalVM JavaScript also provides a console.log function. This is an alias for print in pure JavaScript mode, but uses an implementation provided by Node.js when running in Node mode. The behaviour around Java objects differs for console.log in Node mode as Node.js does not implement special treatment for such objects.