How to use Nashorn in Java 15 and later?

I just released Nashorn 15.1 which makes it possible for Nashorn to function when loaded through classpath instead of through modulepath. I tested it with a small Spring Boot application of my own, and it works.


According to JEP 372, Nashorn had been removed from JDK 15 but you can get latest nashorn from https://search.maven.org/artifact/org.openjdk.nashorn/nashorn-core/15.0/jar

For Maven, include the below dependency into your pom.xml

<dependency>
  <groupId>org.openjdk.nashorn</groupId>
  <artifactId>nashorn-core</artifactId>
  <version>15.0</version>
</dependency>

For Gradle, include dependency below into your build.gradle

implementation 'org.openjdk.nashorn:nashorn-core:15.0'

Unfortunately, Standalone Nashorn is only usable as a JPMS module. So you might need to follow the solution stated in https://stackoverflow.com/a/46289257 to make it work with a non-modular application.

From the given class xxxxx.yyyy.service.JavaScriptServiceImpl and based on feedback from @JornVernee and @AttilaSzegedi, the command line should look like

jdk-15.0.1/bin/java -classpath /home/nashorn-helloworld/target/classes --module-path /home/org/openjdk/nashorn/nashorn-core/15.0:/home/org/ow2/asm/asm/7.3.1:/home/org/ow2/asm/asm-analysis/7.3.1:/home/org/ow2/asm/asm-commons/7.3.1:/home/org/ow2/asm/asm-tree/7.3.1/home/org/ow2/asm/asm-util/7.3.1 --add-modules org.openjdk.nashorn xxxxx.yyyy.service.JavaScriptServiceImpl