如何从 java 的类路径外部动态加载 Clojure 脚本?
我希望允许用户定义的 Clojure 脚本与我的 Java 应用程序交互。问题是,我事先不知道 Clojure 脚本的位置,因此在运行应用程序时无法将它们包含在类路径中。
如何从类路径外部动态加载 Clojure 脚本?
我尝试过一个简单的例子:
RT.loadResourceScript("test.clj");
Var foo = RT.var("user", "foo");
Object result = foo.invoke("Hi", "there");
System.out.println(result);
使用一个看起来像这样的 test.clj:
(ns user)
(defn foo [a b]
(str a " " b))
但没有运气。
我认为它与 RT.makeClassLoader()
或 RT.baseLoader()
以及使用返回的加载器加载 clojure 文件有关,但我似乎无法使它起作用了。 (我不断收到ClassNotFound
)我可能可以通过clojure.lang.RT
的javadoc,但我就是找不到它们。
I wish to enable user defined Clojure scripts to interact with my 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.
How do I dynamically load a Clojure script from outside of my classpath?
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:
(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. (I keep getting ClassNotFound
) I could probably muddle through the javadoc for the clojure.lang.RT
, but I simply could not find them.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试 clojure.lang.Compiler.loadFile(String file)
Try
clojure.lang.Compiler.loadFile(String file)
只要它们依赖于类路径中的内容,您就可以将文件作为字符串读取并对其进行评估,
read-string 从流中读取一个对象,因此您需要将所有内容包装在列表中以使其成为一个对象。
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,
read-string read one object from the stream so you need to wrap everthing in a list to make it one object.