Setting The Classpath For Nashorn Script in jjs / Java 8

The Java 8 jjs interpreter accepts the -cp / -classpath argument. After setting the classpath, all the classes become available from within the executable script.

A utility class com.airhacks.naslib.Inspector in the naslib.jar (this is an sample jar) becomes available for Nashorn after setting the jjs classpath:

#!/usr/bin/jjs -cp ./naslib/target/naslib.jar -fv
//built-in variable
var args = $ARG;

var Inspector = com.airhacks.naslib.Inspector;
//class from jar, calling a static method

In case this post looks a bit crazy to you, you would enjoy the Java 8 / Java EE 7 "More Power with Less Code" workshop :-).

NEW online workshop: Web Components training (online)

Airport MUC workshops: Java EE 8, Java 9: Bootstrap, Effective, Architectures, Single Page Apps, Progressive Web Apps, HTML 5, ES 6, CSS 3 and Microservices

Podcast: and newsletter:

A book about rethinking Java EE Patterns


Hi Adam,
Have you tried it on Linux? It seems that the complite shebang options are interpreted as one option.

Posted by Albert on February 02, 2015 at 02:21 PM CET #

The syntax you used does not work for me on Linux. Try this instead:

#!/usr/bin/jjs -Dnashorn.args=-cp ./naslib/target/naslib.jar -fv

Posted by skanga on August 19, 2016 at 02:54 AM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed
Online Workshops
...the last 150 posts
...the last 10 comments