21

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?

prismofeverything
  • 8,799
  • 8
  • 38
  • 53
  • possible duplicate of [Java and Clojure with Leiningen](http://stackoverflow.com/questions/5432163/java-and-clojure-with-leiningen) – Asfand Qazi Jul 21 '14 at 10:49

3 Answers3

25

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.

Community
  • 1
  • 1
skuro
  • 13,414
  • 1
  • 48
  • 67
  • Although I use [cake](http://clojure-cake.org/) and thus don't have to worry about this, my understanding is that newer versions of Lein have javac functionality baked in, without plugins. – amalloy Sep 22 '11 at 17:08
  • Indeed leiningen merged `javac-plugin` into its main codebase, it's not really a plugin anymore. I'll update the answer to reflect that. – skuro Sep 22 '11 at 20:51
  • This answer is out-of-date. Lein now uses :java-source paths as described by kiko and benkay. – Alan Thompson Feb 06 '14 at 02:56
12

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"]
  ...)
FUD
  • 5,114
  • 7
  • 39
  • 61
Kiko Fernandez
  • 857
  • 10
  • 26
8

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/"]
    ...
)
benkay
  • 589
  • 5
  • 14