如何从 java 的类路径外部动态加载 Clojure 脚本?

发布于 2024-09-12 07:35:21 字数 635 浏览 1 评论 0原文

我希望允许用户定义的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撩动你心 2024-09-19 07:35:21

尝试 clojure.lang.Compiler.loadFile(String file)

Try clojure.lang.Compiler.loadFile(String file)

蓝戈者 2024-09-19 07:35:21

只要它们依赖于类路径中的内容,您就可以将文件作为字符串读取并对其进行评估,

(def content "(ns user) (defn foo [a b] (str a \" \" b))")
(map eval (read-string (str \( content \))))

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,

(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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文