How do I recompile and reload Java source code while `lein repl` is running?
Asked Answered
R

5

15

I have a Clojure project, and I'm using leiningen. I'm also using tools.namespace to reload Clojure code while running a REPL. If I want to include Java source in the project, can I recompile and reload it while the REPL is running? What is the most convenient/dynamic way of doing it? Can I do it so that it works well with tools.namespace?

Rafaelita answered 9/12, 2013 at 8:23 Comment(4)
Maybe add :aot :all in project.clj?Chucklehead
@Chucklehead that does not sound like what I want.Rafaelita
I'm looking for the exact same thing. It'll be a huge win if this functionality is implemented. There is a javac function in github.com/technomancy/leiningen/blob/… that is included in leiningenEinsteinium
Does #5432663 help?Avoirdupois
E
7

I'm answering my own bounty here but I did do a bit of work getting this up:

Use Vinyasa,

and here is a blog post:

Dynamic reloading of java code in emacs/nrepl

... actually... it's kinda not working anymore... you have to do back to the earlier versions in order to get the support.

Einsteinium answered 3/1, 2014 at 3:18 Comment(2)
This library has been completely removed, as well as it's replacement, Lucidity.Byronbyrum
It's still on clojars ... just very much unsupported. virgil's definitely the better option right now.Einsteinium
A
6

Nowadays (2016->) the better answer is to use Virgil. It watches and recompiles all Java code in your leiningen project automatically in the background, as opposed to Vinyasa's approach of invoking reimport.

Agbogla answered 4/10, 2016 at 17:2 Comment(1)
You don't get nearly enough credit for this, this is definitely the correct answer now :)Byronbyrum
R
3

Spring-loaded or JRebel might be what you want. Have a look at https://github.com/spring-projects/Spring-loaded or http://zeroturnaround.com/software/jrebel/. They both provide an agent monitoring the filesystem for class file changes and update class definitions in the running JVM. I personally use Spring-loaded, but not yet together with tools.namespace. I guess the key to run them both is to make sure they do not conflict. So if you use Spring-loaded, it should be the only tool tracking class files and you better not use aot at all. If I remember correctly, tools.namespace discourages the use of aot anyways.

Radnorshire answered 10/12, 2013 at 5:38 Comment(0)
N
2

Pure java way

public class MyClassFactory {
   public static MyClass newInstance() {
       URLClassLoader cl =
           new URLClassLoader(new URL[] {getMyClassPath()}) {

           public Class loadClass(String name) {
              if ("MyClass".equals(name))
                 return findClass(name);
              return super.loadClass(name);
          }
       };

     return (MyClass) cl.loadClass("MyClass").newInstance();
  }
}

by this way you can lead the class loader to load classes programmatically.

References

Natterjack answered 5/1, 2014 at 8:51 Comment(0)
D
0

See also the official JVM services loader

ServiceLoader

Dearly answered 5/1, 2014 at 9:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.