Classpath resources with Nashorn (JJS)

Edgar Merino donvodka at gmail.com
Mon Sep 26 05:16:46 UTC 2016


Hello:


     I don't really know if this belongs here, I couldn't find much 
documentation on this issue.

     I've noticed that, when using nashorn's command line utility JJS, 
any resource that is included in the classpath, be it included in a JAR 
or located in a classpath folder, is not available to scripts. A simple 
example:

|echo "java.lang.Class.class.getResourceAsStream('/test.txt')"|jjs -cp .|

     When a file "test.txt" is included in the current folder (which is 
added to the classpath via the -cp option), this code always returns 
"null". If I run a similar example using java and the jvm directly I get 
the expected resource (an input stream reference). Under rhino I get the 
same results, using a similar example.

     Is this the expected behavior? Is there a way to access resources 
under the classpath in nashorn? This is causing a lot of problems with 
spring contexts for example, everytime I try to load my app context from 
an XML file I get problems because it cannot find some needed resources 
at runtime. This worked fine under rhino, but we are planning on 
migrating to nashorn.

I found this github wiki, were it states loading resources is only 
possible for nashorn's app class loader: 
https://github.com/DaniHaag/snippets/wiki/Nashorn 
<https://github.com/DaniHaag/snippets/wiki/Nashornhttps://github.com/DaniHaag/snippets/wiki/Nashorn>

This related question is given a useful answer, but it involves creating 
the nashorn engine programatically, instead of using JJS: 
http://stackoverflow.com/questions/30225398/java-8-scriptengine-across-classloaders


Any help is greatly appreciated.

Edgar Merino.



More information about the nashorn-dev mailing list