简体   繁体   English

如何从java的classpath外部动态加载Clojure脚本?

[英]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.

相关问题 如何从Clojure动态加载Java AppleScript引擎? - How to load java AppleScript engine dynamically from Clojure? Java反思:如何在不将另一个项目或JAR添加到我的类路径的情况下从另一个项目加载一个类? - Java reflection: How can I load a class from another project without adding that project or JAR to my classpath? 如何从 java 类路径中的网络目录正确加载资源? - How do I correctly load resources from a network directory in the java classpath? 如何从类路径加载ChromeDriver二进制文件? - How do I load the ChromeDriver binary from the classpath? 如何从 Java 中的目录动态加载模块 9+ - How do I dynamically load modules from a directory in Java 9+ 如何使用Apache CXF从类路径中加载信任库? - How do I load truststore from classpath with Apache CXF? 如何从我的类路径运行 Liquibase changeLogFile - How do I run a Liquibase changeLogFile from my classpath 如何从java检查PATH和CLASSPATH环境变量? - How Do I check PATH and CLASSPATH environment variables from java? 如何从类路径中的 Java 包中读取所有类? - How do I read all classes from a Java package in the classpath? Spring Boot-从外部类路径加载文件 - Spring boot - load file from outside classpath
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM