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

前端 未结 5 1130
[愿得一人]
[愿得一人] 2021-01-31 12:07

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,

5条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-31 12:29

    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

    • Classloader Official documentation
    • Java Specs
    • OnJava

提交回复
热议问题