Question

I'm just learning how to lein, and I'd like to use from a Java source a class created by deftype in a Clojure source. This wasn't covered in the basic tutorial and I can't get it to work properly.

The problem is that Java source can't import Clojure class, since it hasn't been compiled yet. And Clojure class isn't compiled, since compilation is aborted by the Java source.

I give a minimal example:

  1. Create a new project with:

    lein new app javafoo
    
  2. Add to project.clj

    :aot :all
    :java-source-paths ["src/java"]
    
  3. Put into src/javafoo/core.clj:

    (ns javafoo.core)
    (deftype PPoint [x y])
    
  4. Put into src/java/JavaFoo.java:

    package foo.java;
    import javafoo.core.PPoint;
    
    public class JavaFoo {
        public static void main(String[] args) {
            System.out.println("JavaFoo");
        }
    }
    
  5. Try to compile

    lein compile
    

It fails with package javafoo.core doesn't exist. So now I have to

  1. Comment out :java-source-paths
  2. Compile
  3. Uncomment :java-source-paths
  4. Compile

It finally works. Is there a way to make it work from the start?

Was it helpful?

Solution

Add this line to your project.clj:

:prep-tasks [["compile" "javafoo.core"] "javac"]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top