3

I need to compile some clojure file into java bytecode files, store them on the disk, so I would be able to load/execute them later.

I tried the following:

RT.load("clojure/core");
RT.init();
String clazz = "(ns org.rogach.avalanche.build (:gen-class)) (defn -doStuff [] (println 1))";
Compiler.load(new StringReader(clazz));
Compiler.load(new StringReader("(compile 'org.rogach.avalanche.build)"));

But it fails:

Exception in thread "main" java.io.FileNotFoundException: 
Could not locate org/rogach/avalanche/build__init.class or org/rogach/avalanche/build.clj on classpath

To be fair, exactly the same thing happens if I execute that code from clojure REPL directly.

Also, this approach doesn't let me to specify output directory for class files, which also important.

How can I compile that clojure code to classfiles?

5
  • Curious: why do you need to compile the Clojure code into class files? AOT compilation adds a lot of complications, normally it is easier to just compile/load Clojure code from .clj files at runtime. Sounds a bit like an "XY problem" to me... Commented Jun 21, 2013 at 19:44
  • @mikera - I have a program, that takes clojure file as a config file. I already know how to eval that code, but that requires initializing the clojure runtime, and that takes ~1 second - I hoped to work around that by compiling the config only when it is changed, and if it is not changed, simply load and execute pre-compiled class files. Commented Jun 22, 2013 at 6:59
  • Then an answer to this question won't help you.... loading and initialising clojure.core is almost certainly what causes the startup time, not loading and compiling your config file. Commented Jun 22, 2013 at 7:15
  • So there's absoultely no way to execute clojure code without ~1 second overhead? There are no ways to speed it up? Commented Jun 22, 2013 at 7:26
  • Clojure will always have a longish startup time until someone revamps the compiler / bootstrapping code. My recommended solution right now (if you care about latency) is to keep a persistent JVM / REPL instance running so that startup time isn't a concern. Commented Jun 22, 2013 at 13:30

2 Answers 2

2

I strongly suspect the answer is: not directly.

The unit of AOT compilation is the namespace, and the compile function seems to look for a namespace in a file. I think it is necessarily to write the Clojure files to the correct relative path so the compile function can find the appropriate namespace.

Sign up to request clarification or add additional context in comments.

1 Comment

Is there no way to hack deep into compiler API to compile? I don't mind duplicating some code from clojure compiler, I just don't know where to look.
0

Look at Programmatically compiling java code using clojure by Nakkaya. I've used it in the past, and was the one who made the comment on how to extend it to interfaces. I'm not sure about your use-case, but this does what you asked.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.