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
Inspector.inspect(args);

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: WebStandards Igniter (online)

Airport MUC workshops: Java EE 7: Bootstrap, Effective, Architectures, Web, React and Angular, Testing and Microservices

Podcast: airhacks.fm and newsletter: airhacks.news

A book about rethinking Java EE Patterns

Comments:

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

http://stackoverflow.com/questions/28252126/running-nashorn-using-shebang-does-not-accept-cp-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
realworldpatterns.com
Online Workshops
...the last 150 posts
...the last 10 comments
License