[英]How do I dynamically load a Clojure script from outside of my classpath from java?
I wish to enable user defined Clojure scripts to interact with my Java App. 我希望启用用户定义的Clojure脚本来与我的Java App进行交互。 The problem is, I don't know in advance where the Clojure scripts will be located, so I can't include them in my classpath when running the app. 问题是,我事先并不知道Clojure脚本的位置,所以在运行应用程序时我不能将它们包含在我的类路径中。
How do I dynamically load a Clojure script from outside of my classpath? 如何从类路径外部动态加载Clojure脚本?
I've tried the simple example: 我试过这个简单的例子:
RT.loadResourceScript("test.clj");
Var foo = RT.var("user", "foo");
Object result = foo.invoke("Hi", "there");
System.out.println(result);
with a test.clj that looks like: 使用看起来像的test.clj:
(ns user)
(defn foo [a b]
(str a " " b))
But no luck. 但没有运气。
I think it has something to do with RT.makeClassLoader()
or RT.baseLoader()
and using the returned loader to load the clojure file, but I cannot seem to make it work. 我认为它与RT.makeClassLoader()
或RT.baseLoader()
并使用返回的加载器加载clojure文件,但我似乎无法使它工作。 (I keep getting ClassNotFound
) I could probably muddle through the javadoc for the clojure.lang.RT
, but I simply could not find them. (我一直在使用ClassNotFound
)我可能会混淆clojure.lang.RT
的javadoc,但我根本找不到它们。
试试clojure.lang.Compiler.loadFile(String file)
As long as they depend on the stuff in your classpath what you can do is read the file as a string and evaluate it, 只要它们依赖于类路径中的东西,你可以做的就是将文件作为字符串读取并进行评估,
(def content "(ns user) (defn foo [a b] (str a \" \" b))")
(map eval (read-string (str \( content \))))
read-string read one object from the stream so you need to wrap everthing in a list to make it one object. read-string从流中读取一个对象,因此您需要在列表中包含everthing以使其成为一个对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.