0

In my Eclipse project I have the following Clojure code in src/main/clojure/ext/clojuretest/helloworld.clj:

(ns ext.clojuretest.helloworld)

;; https://clojuredocs.org/clojure.core/gen-class
(gen-class
  :name "ext.clojuretest.HelloWorld"
  :prefix "java-"
  :main true)

(defn java-main [& args]
  "I can say 'Hello World'."
  (println "Hello, World from Clojure!"))
  ;;(.println (System/out) "Hello, World from Java!"))

I'd like to use the class from Java code within the same project: src/main/java/ext/clojuretest/ClojureTest.java:

package ext.clojuretest;

public class ClojureTest {

    public static void main(String[] args) {
        HelloWorld h = new HelloWorld();
    }

}

I'm Building my Project with Gradle (build.gradle):

plugins {
  id "de.kotka.clojuresque.clojure" version "2.0.0"
}

repositories {
  mavenCentral()
}

dependencies {
    compile 'org.clojure:clojure:1.8.0'
}

clojure.aotCompile = true

Is it somehow possible to use the class HelloWorld from within my Java code? Currently the compiler can't find HelloWorld

HelloWorld cannot be resolved to a type
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Edward
  • 4,453
  • 8
  • 44
  • 82
  • Did you compile the clojure code? – OneCricketeer Jul 29 '16 at 13:43
  • Have you seen [this answer](http://stackoverflow.com/a/23555959/5044950) that details the preferred way to call Clojure from Java? – Sam Estep Jul 29 '16 at 13:44
  • @cricket_007 In my Build-Script I tried: `compileJava.dependsOn compileClojure` – Edward Jul 29 '16 at 13:49
  • @SamEstep : I dont't see how this post can solve my problem. What is the `RT` and the `IFn` class for?; I also don't want to call a clojure function; I'd like to generate a class (which actually implements a Java interface). And use a method of this class. – Edward Jul 29 '16 at 13:59
  • @Edward That answer specifically states that the `RT` class is not meant to be called directly. Also, if your goal is to implement a Java interface, why doesn't the `gen-class` in your question use `:implements`? – Sam Estep Jul 29 '16 at 14:07
  • @SamEstep : I left that part out, to keep the example simple – Edward Jul 29 '16 at 14:11
  • @Edward The point is that your question makes it seem like the easiest solution to your problem would be to just put `(ns ext.clojuretest.helloworld) (defn foo [] (println "Hello, world!"))` in `helloworld.clj` and call that function from Java using the API detailed in the answer I linked. If, as your comments indicate, such a solution would *not* work in your case, you should replace the code currently in your question with something that demonstrates what you're really trying to do and why the linked answer doesn't apply. – Sam Estep Jul 29 '16 at 14:18
  • @SamEstep : Apart from the best practice mentioned in the linked article -> Do you have any idea why my code is not working? – Edward Jul 29 '16 at 14:59
  • @Edward My guess is that, like cricket_007 said, you didn't compile your Clojure code correctly. Incidentally, as far as I am aware, although `gen-class` requires AOT compilation to work, the API detailed in Alex Miller's answer does not. – Sam Estep Jul 29 '16 at 15:03

0 Answers0