I have a java file with a single class and I want to include it in my lein project. Where do I put it and how do I import it? (I tried putting it in the src directory under the package path but it tells me ClassNotFound)
So the java file has this package declaration:
package com.thebuzzmedia.imgscalr;
and has this class:
public class Scalr {
I put it in ~/src/com/thebuzzmedia/imgscalr/Scalr.java and tried to import it from the repl thusly:
(import '(com.thebuzzmedia.imgscalr Scalr))
And I get this:
com.thebuzzmedia.imgscalr.Scalr
[Thrown class java.lang.ClassNotFoundException]
What am I missing?
Where to place Java sources really depends on which build system you're using. If you're using Leiningen, you have to configure the source paths:
(defproject my-project "0.0.1-SNAPSHOT"
[...]
:java-source-paths ["src/java" "test/java"])
Then you can import Java classes at those source locations in your code or at the REPL like you were already trying to do.
As of Leiningen 2.X, :java-source-path
has been replaced with :java-source-paths
, whose value is now specified as a vector rather than a string.
Example:
(defproject my-project "0.0.1-SNAPSHOT"
[...]
:java-source-paths ["src/main/java" "src/main/test"]
...)
Since I do not have the reputation to comment on the above answers, I am left with no recourse but to leave my own, ever-so-slightly different answer.
The correct syntax (as of Leiningen 2.1.3) is:
(defproject
...
:java-source-paths ["src/main/java/" "foo/bar/baz/"]
...
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With