从 java 调用 clojure 函数但无法找到类路径
Calling clojure function from java but unable to find classpath
我正在尝试从 java class 调用 clojure 函数,但即使 clojure 文件与 java class 位于同一位置,它的投掷
Exception in thread "main" java.io.FileNotFoundException: Could not
locate com/foo/names__init.class or
com/foo/names.clj on classpath.
这是我的项目结构:
src
main
java
com
foo
names.clj
TestClj.java
names.clj
(ns com.foo.names)
(defn add-me
[one two]
(+ one two))
TestClj.java
public class TestClj {
private static final IFn REQUIRE = Clojure.var("clojure.core", "require");
public TestClj(String ns) {
this.ns = ns;
REQUIRE.invoke(Clojure.read(ns)); <- Breaks here
}
public static void main(String[] args) {
TestClj clj = new TestClj("com.foo.names");
Clojure.var("com.foo.names", "add-me").invoke(1, 2);
}
}
我在这里错过了什么?
编辑 添加 pom 文件
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.clojure</groupId>
<artifactId>clojure</artifactId>
<version>1.7.0-alpha5</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.clojure</groupId>
<artifactId>clojure</artifactId>
<scope>compile</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>clojars.org</id>
<name>Clojars repository</name>
<url>https://clojars.org/repo</url>
</repository>
</repositories>
看起来您正在使用 Maven 构建一个 Java 项目,同时还混入了一点 Clojure。这应该可行,但需要与您当前拥有的布局略有不同。如异常消息中所示,Clojure 的编译器希望在 com/foo/names.clj
.
的类路径上找到命名空间 com.foo.names
的源代码作为资源
在Maven standard directory layout中src/main/java
是源文件所在的地方,并没有添加到项目的类路径中。对于类路径中确实需要的资源文件,您需要 src/main/resources
:
Within artifact producing source directories (ie. main and test), there is one directory for the language java (under which the normal package hierarchy exists), and one for resources (the structure which is copied to the target classpath given the default resource definition).
尝试将 Clojure 文件放在 src/main/resources/com/foo/names.clj
。这应该可以解决导致异常的直接原因。
如果如评论中所述,您希望项目的 Clojure 代码本身被视为源代码,请查看 Clojure Maven plugin。这应该允许您将 Clojure 源代码放在 src/main/clojure
下,并与 Java 代码一起编译,编译后的 Clojure 代码在类路径中可用。
如果您的项目主要使用 Clojure,那么 Clojure 生态系统中最常见的构建框架是 Leiningen,您可能还想看看它。
我正在尝试从 java class 调用 clojure 函数,但即使 clojure 文件与 java class 位于同一位置,它的投掷
Exception in thread "main" java.io.FileNotFoundException: Could not locate com/foo/names__init.class or com/foo/names.clj on classpath.
这是我的项目结构:
src
main
java
com
foo
names.clj
TestClj.java
names.clj
(ns com.foo.names)
(defn add-me
[one two]
(+ one two))
TestClj.java
public class TestClj {
private static final IFn REQUIRE = Clojure.var("clojure.core", "require");
public TestClj(String ns) {
this.ns = ns;
REQUIRE.invoke(Clojure.read(ns)); <- Breaks here
}
public static void main(String[] args) {
TestClj clj = new TestClj("com.foo.names");
Clojure.var("com.foo.names", "add-me").invoke(1, 2);
}
}
我在这里错过了什么?
编辑 添加 pom 文件
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.clojure</groupId>
<artifactId>clojure</artifactId>
<version>1.7.0-alpha5</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.clojure</groupId>
<artifactId>clojure</artifactId>
<scope>compile</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>clojars.org</id>
<name>Clojars repository</name>
<url>https://clojars.org/repo</url>
</repository>
</repositories>
看起来您正在使用 Maven 构建一个 Java 项目,同时还混入了一点 Clojure。这应该可行,但需要与您当前拥有的布局略有不同。如异常消息中所示,Clojure 的编译器希望在 com/foo/names.clj
.
com.foo.names
的源代码作为资源
在Maven standard directory layout中src/main/java
是源文件所在的地方,并没有添加到项目的类路径中。对于类路径中确实需要的资源文件,您需要 src/main/resources
:
Within artifact producing source directories (ie. main and test), there is one directory for the language java (under which the normal package hierarchy exists), and one for resources (the structure which is copied to the target classpath given the default resource definition).
尝试将 Clojure 文件放在 src/main/resources/com/foo/names.clj
。这应该可以解决导致异常的直接原因。
如果如评论中所述,您希望项目的 Clojure 代码本身被视为源代码,请查看 Clojure Maven plugin。这应该允许您将 Clojure 源代码放在 src/main/clojure
下,并与 Java 代码一起编译,编译后的 Clojure 代码在类路径中可用。
如果您的项目主要使用 Clojure,那么 Clojure 生态系统中最常见的构建框架是 Leiningen,您可能还想看看它。