Let's say we have a Java project in IntelliJ Idea with La Clojure
plugin installed and clojure.jar
library imported as External Library.
I would like then to add a Clojure file.clj
into our Java Project structure and use functions from this file for my further internal purposes.
How to create a clojure file in a Java project and then use clojure code from there? I've looked through a couple of variants but it didn't work for me or just wasn't enough. Even on YouTube there're plenty of dummy videos just showing how to run a repl but nothing about how to stick java and clojure together and turn this to easy, enjoyable and convinient programming discipline. For example:
input.clj:
(defn foo [a b] (str a " " b))
Hello.java
public class Hello {
psvm {
//invoke: (foo "Hello from file.clj")
}
}
Update: Thanks for your help, just started using Clojure 1.6.0 and didn't expect it would be that easy.
/** Question: If I won't declare and init IFn, then Java throws "Exception
* in thread "main" java.lang.ExceptionInInitializerError" during an attempt
* to read the file. Why? And how not to use IFn if not really necessary?
*/
IFn require = Clojure.var("clojure.core", "require");
// Import file input.clj with a bunch of functions
clojure.lang.Compiler.loadFile("input.clj");
// Call foo
IFn foo = Clojure.var("clojure.core", "foo");
Object result = foo.invoke("Hi", "there");
// Feel good
System.out.println(result);