I am a fledgling Clojure programmer experienced wielding straight-up-the-middle Java in eclipse. I am trying to get my Java program to call the simple "hello" function created from the http://dev.clojure.org/display/doc/Getting+Started+with+Eclipse+and+Counterclockwise article. The hello function works fine from a Clojure REPL launched through the counterclockwise plug-in. Problems arise when I try to execute the hello function from a Java class.
Googling around reveals that there are basically 2 ways to do this: clojure.lang.RT can load the Clojure source and execute it as a script, or directly when the Clojure source was compiled into a JAR.
The clojure.lang.RT variant is working without problem, but I am completely at a loss as to how to get the direct invocation variant working. In the Java file, the compiler cannot resolve "myproject.core".
The Clojure source is core.clj and is as follows and works like a champ through the REPL:
(ns myproject.core
(:gen-class
:name myproject.core
:methods [#^{:static true} [hello [String] String]]))
(defn -main
"I don't do a whole lot."
[& args]
(println "Hello, World!"))
(defn hello [who] (str "Hello " who " !"))
The Java source, however, won't compile:
import java.io.*;
import clojure.lang.*;
public class HelloJava {
public static void main( String[] args ) {
loadResourceVariant();
directVariant();
}
public static void directVariant() {
myproject.core.hello( "Bob" );
}
public static void loadResourceVariant() {
try {
RT.loadResourceScript( "myproject/core.clj" );
// Get a reference to the hello function.
Var hello = RT.var( "myproject.core", "hello" );
// Invoke the hello function
Object result = hello.invoke( "Robert" );
System.out.println( result );
} catch ( IOException e ) {
e.printStackTrace();
}
}
}
The compiler error is...
myproject.core cannot be resolved to a type HelloJava.java /myproject/src line 11 Java Problem
How do I configure my counterclockwise project to put the .class representation of core.clj into the classes directory so it can be directly referenced from Java?
This has to be possible without going full-on Maven and the like. No?
The way I am calling clojure from java is by making an api namespace, usually separated from the implementing namespace:
then I make sure to AOT that namespace, and uberjar it.
now just include that jar in your java classpath, and you'r ready to go.