How to claim Java classes in clojure / leiningen
I am testing Clojure / Java interop. I created a new Leiningen project and created this project.clj file with several dependencies that I will 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 the hello.java file in the src / java directory, which 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 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 don't even use the hello java class in the application, I just require 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 do differently?
+3
source to share
1 answer
Check out my answer here, this is pretty much what you want, I think: fooobar.com/questions/484854 / ...
Tips:
- don't use the bare package structure - you probably can, but this usually leads to unwanted problems.
- Java classes are imported, not required. The import is only needed when you don't want to enter a package prefix.
+5
source to share