Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clojure/lein: How do I include a java file in my project?

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?

like image 585
prismofeverything Avatar asked Sep 22 '11 08:09

prismofeverything


3 Answers

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.

like image 195
skuro Avatar answered Nov 10 '22 18:11

skuro


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"]
  ...)
like image 20
Kiko Fernandez Avatar answered Nov 10 '22 19:11

Kiko Fernandez


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/"]
    ...
)
like image 8
benkay Avatar answered Nov 10 '22 20:11

benkay