How to require java classes in clojure/leiningen

2019-05-14 05:59发布

问题:

I am testing out Clojure/Java interop. I created a new Leiningen project and created this project.clj file with a few dependencies I'll be using:

    (defproject kente "0.1.0-SNAPSHOT"
      :description "FIXME: write description"
      :url "http://example.com/FIXME"
      :license {:name "Eclipse Public License"
                :url "http://www.eclipse.org/legal/epl-v10.html"}
      :dependencies [[org.clojure/clojure "1.4.0"]
                     [ring/ring-jetty-adapter "1.0.0-RC1"]
                     [compojure "0.6.5"]
                     [hiccup "0.3.7"]
                     [cheshire "5.0.1"]]
      :java-source-paths ["src/java"]
      :plugins [[lein-ring "0.8.2"]]
      :ring {:handler kente.core/application}
      :main kente.core)

I also included the "java-source-paths" list and put a hello.java file into the src/java directory that looks like this:

public class hello {

    private String msg = "Hello World";

    public String sayHello() {
        return this.msg;
    }

}

and then I created this core.clj file from the template:

    (ns kente.core
      (:require [java/hello]))

    (defn application
      "I don't do a whole lot."
      [x]
      (println x "Hello, World!"))

    (defn -main [] (application "Say: "))

As you can see, I'm not even using the hello java class in the application function, I'm just requiring it, but I get this stack trace when I run "lein jar" in the terminal:

    $ lein jar
    Compiling kente.core
    Exception in thread "main" java.io.FileNotFoundException: Could not locate hello__init.class or hello.clj on classpath: , compiling:(core.clj:1)
            at clojure.lang.Compiler$InvokeExpr.eval(Compiler.java:3387)
            at clojure.lang.Compiler.compile1(Compiler.java:7035)
            at clojure.lang.Compiler.compile1(Compiler.java:7025)
            at clojure.lang.Compiler.compile(Compiler.java:7097)
            at clojure.lang.RT.compile(RT.java:387)
            at clojure.lang.RT.load(RT.java:427)
            at clojure.lang.RT.load(RT.java:400)
            at clojure.core$load$fn__4890.invoke(core.clj:5415)
            at clojure.core$load.doInvoke(core.clj:5414)
            at clojure.lang.RestFn.invoke(RestFn.java:408)
            at clojure.core$load_one.invoke(core.clj:5227)
            at clojure.core$compile$fn__4895.invoke(core.clj:5426)
            at clojure.core$compile.invoke(core.clj:5425)
            at user$eval7.invoke(NO_SOURCE_FILE:1)
            at clojure.lang.Compiler.eval(Compiler.java:6511)
            at clojure.lang.Compiler.eval(Compiler.java:6501)
            at clojure.lang.Compiler.eval(Compiler.java:6477)
            at clojure.core$eval.invoke(core.clj:2797)
            at clojure.main$eval_opt.invoke(main.clj:297)
            at clojure.main$initialize.invoke(main.clj:316)
            at clojure.main$null_opt.invoke(main.clj:349)
            at clojure.main$main.doInvoke(main.clj:427)
            at clojure.lang.RestFn.invoke(RestFn.java:421)
            at clojure.lang.Var.invoke(Var.java:419)
            at clojure.lang.AFn.applyToHelper(AFn.java:163)
            at clojure.lang.Var.applyTo(Var.java:532)
            at clojure.main.main(main.java:37)
    Caused by: java.io.FileNotFoundException: Could not locate hello__init.class or hello.clj on classpath:
            at clojure.lang.RT.load(RT.java:432)
            at clojure.lang.RT.load(RT.java:400)
            at clojure.core$load$fn__4890.invoke(core.clj:5415)
            at clojure.core$load.doInvoke(core.clj:5414)
            at clojure.lang.RestFn.invoke(RestFn.java:408)
            at clojure.core$load_one.invoke(core.clj:5227)
            at clojure.core$load_lib.doInvoke(core.clj:5264)
            at clojure.lang.RestFn.applyTo(RestFn.java:142)
            at clojure.core$apply.invoke(core.clj:603)
            at clojure.core$load_libs.doInvoke(core.clj:5298)
            at clojure.lang.RestFn.applyTo(RestFn.java:137)
            at clojure.core$apply.invoke(core.clj:603)
            at clojure.core$require.doInvoke(core.clj:5381)
            at clojure.lang.RestFn.invoke(RestFn.java:408)
            at kente.core$loading__4784__auto__.invoke(core.clj:1)
            at clojure.lang.AFn.applyToHelper(AFn.java:159)
            at clojure.lang.AFn.applyTo(AFn.java:151)
            at clojure.lang.Compiler$InvokeExpr.eval(Compiler.java:3382)
            ... 26 more

What should I be doing differently?

回答1:

Check out my answer here, it's pretty much what you want I think: https://stackoverflow.com/a/14014153/6264

Hints:

  • don't use a naked package structure - maybe you can, but it generally leads to unwanted problems
  • Java classes get imported, not required. Importing is only needed when you don't want to type the package prefix.