Can I use Nashorn boot scripts from classpath?

Is it possible for the Nashorn method to loaduse the project class path when resolving the URI?

Here is what I am trying to do:

(defn create-engine
  "Creates a new nashorn script engine and loads dependencies into its context."
  [dependencies]
  (let [nashorn (.getEngineByName (ScriptEngineManager.) "nashorn")
        scripts (map #(str "load('" % "');") dependencies)]
    (.eval nashorn "var global = this;")
    (doseq [script scripts] (.eval nashorn script))
    nashorn))

(def app "public/javascripts/app.js") ; in /resouces, on classpath


; resulting exception:
clojure.lang.Compiler$CompilerException: 
javax.script.ScriptException: TypeError: 
Cannot load script from public/javascripts/app.js in <eval> at line number 1
+4
source share
3 answers

The absolute path should always start with a slash "/public/javascripts/app.js", but I'm not sure if load()loading the class path, if you don't need it, you might have to use the method eval(Reader)and build the reader yourself

EDIT:

, . , . InputStream eval reader, Java :

nashorn.eval (new InputStreamReader (getClass().getResourceAsStream("/public/javascripts/app.js")));

clojure, .

EDIT2:

, :

(defn create-engine
  "Creates a new nashorn script engine and loads dependencies into its context."
  [dependencies]
  (let [nashorn (.getEngineByName (ScriptEngineManager.) "nashorn")]
    (.eval nashorn "var global = this;")
    (doseq [dependency dependencies] (.eval nashorn (new InputStreamReader (.getResourceAsStream String dependency))))
    nashorn))

, -

+2

clojure.java.io/resource

(def nashorn (.. (javax.script.ScriptEngineManager.) (getEngineByName "nashorn")))
(.eval nashorn (clojure.java.io/reader (clojure.java.io/resource "jvm-npm.js")))

, lein:

:resource-paths ["src/resources"]
0

All Articles