简体   繁体   中英

Calling clojure function from java but unable to find classpath

I am trying to call a clojure function from a java class but even though the clojure file is located at the same place as the java class, its throwing

Exception in thread "main" java.io.FileNotFoundException: Could not locate com/foo/names__init.class or com/foo/names.clj on classpath.

This is my project structure:

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);
  }  
}

What am I missing here?

EDIT Adding the pom file

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

It looks like you're using Maven to build a Java project, with a little bit of Clojure sprinkled in alongside. This should work, but will require a slightly different layout than what you currently have. As indicated in the exception message, Clojure's compiler expects to find the source code for namespace com.foo.names as a resource on the classpath at com/foo/names.clj .

In the Maven standard directory layout src/main/java is where source files go, and is not added to the project's classpath. For resource files that you do want on the classpath, you want 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).

Try putting the Clojure file at src/main/resources/com/foo/names.clj . This should fix the immediate cause of your exception.

If, as indicated in the comments, you want your project's Clojure code to be treated as source code in its own right, then take a look at the Clojure Maven plugin . This should allow you to put Clojure source code under src/main/clojure and have it compiled alongside the Java code, with the compiled Clojure code available on the classpath.

If your project is primarily in Clojure, the most common build framework in the Clojure ecosystem is Leiningen , which you might also want to take a look at.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM