Search code examples
clojureleiningenclojure-java-interop

leiningen: ClassNotFoundException for class present in project on “lein run” / “lein uberjar”


I am trying to implement a Java interface that is required by a legacy runtime I am using and instantiate an instance of this implementation to pass it to the runtime. But when I run lein uberjar I see an exception, that the class cannot be found. Both namespaces are defined in the same leiningen project, so I would have expected, that they see each other as well as the classes generated by them.

(ns man.core                                                                     
  (:require (man.gateway))                                                       
  (:import (man ManGate)                                                         
           (eu.m2machine.gw GatewayComponentFactory))                            
  (:gen-class))                                                                  

(defn -main [& args]                                                             
  (let [starter (.starter (GatewayComponentFactory/get))                         
        gateway (ManGate.)]                                                      
    (.startup starter gateway)))

This code tries to use a class implemented in the same project:

(ns man.gateway                                                                  
  (:import [eu.m2machine.gw.text GatewayIDFormatter])                            
  (:gen-class                                                                    
   :name man.ManGate                                                             
   :implements [eu.m2machine.gw.Gateway]                                         
   :prefix "gateway-"))                                                          

(defn gateway-startup [this])                                                    

(defn gateway-shutdown [this])

The two methods required by the interface only have stub implementations so far. They would get their code after I can compile the code. The interface is defined (in an artifact added as dependency):

package eu.m2machine.gw;                                                         

public interface Gateway {                                                       
    void startup();                                                              
    void shutdown();                                                             
}

The exception I get is:

Exception in thread "main" java.lang.ClassNotFoundException: man.ManGate, compiling:(man/core.clj:1:1)
    at clojure.lang.Compiler.load(Compiler.java:7391)
    at clojure.lang.RT.loadResourceScript(RT.java:372)
    at clojure.lang.RT.loadResourceScript(RT.java:363)
    at clojure.lang.RT.load(RT.java:453)
    at clojure.lang.RT.load(RT.java:419)
    at clojure.core$load$fn__5677.invoke(core.clj:5893)
    at clojure.core$load.invokeStatic(core.clj:5892)
    at clojure.core$load.doInvoke(core.clj:5876)
    at clojure.lang.RestFn.invoke(RestFn.java:408)
    at clojure.core$load_one.invokeStatic(core.clj:5697)
    at clojure.core$load_one.invoke(core.clj:5692)
    at clojure.core$load_lib$fn__5626.invoke(core.clj:5737)
    at clojure.core$load_lib.invokeStatic(core.clj:5736)
    at clojure.core$load_lib.doInvoke(core.clj:5717)
    at clojure.lang.RestFn.applyTo(RestFn.java:142)
    at clojure.core$apply.invokeStatic(core.clj:648)
    at clojure.core$load_libs.invokeStatic(core.clj:5774)
    at clojure.core$load_libs.doInvoke(core.clj:5758)
    at clojure.lang.RestFn.applyTo(RestFn.java:137)
    at clojure.core$apply.invokeStatic(core.clj:648)
    at clojure.core$require.invokeStatic(core.clj:5796)
    at clojure.core$require.doInvoke(core.clj:5796)
    at clojure.lang.RestFn.invoke(RestFn.java:408)
    at user$eval5$fn__7.invoke(form-init2162986879369932757.clj:1)
    at user$eval5.invokeStatic(form-init2162986879369932757.clj:1)
    at user$eval5.invoke(form-init2162986879369932757.clj:1)
    at clojure.lang.Compiler.eval(Compiler.java:6927)
    at clojure.lang.Compiler.eval(Compiler.java:6917)
    at clojure.lang.Compiler.load(Compiler.java:7379)
    at clojure.lang.Compiler.loadFile(Compiler.java:7317)
    at clojure.main$load_script.invokeStatic(main.clj:275)
    at clojure.main$init_opt.invokeStatic(main.clj:277)
    at clojure.main$init_opt.invoke(main.clj:277)
    at clojure.main$initialize.invokeStatic(main.clj:308)
    at clojure.main$null_opt.invokeStatic(main.clj:342)
    at clojure.main$null_opt.invoke(main.clj:339)
    at clojure.main$main.invokeStatic(main.clj:421)
    at clojure.main$main.doInvoke(main.clj:384)
    at clojure.lang.RestFn.invoke(RestFn.java:421)
    at clojure.lang.Var.invoke(Var.java:383)
    at clojure.lang.AFn.applyToHelper(AFn.java:156)
    at clojure.lang.Var.applyTo(Var.java:700)
    at clojure.main.main(main.java:37)
Caused by: java.lang.ClassNotFoundException: man.ManGate
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at clojure.lang.DynamicClassLoader.findClass(DynamicClassLoader.java:69)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at clojure.lang.DynamicClassLoader.loadClass(DynamicClassLoader.java:77)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)
    at clojure.lang.RT.classForName(RT.java:2168)
    at clojure.lang.RT.classForNameNonLoading(RT.java:2181)
    at man.core$eval20$loading__5569__auto____21.invoke(core.clj:1)
    at man.core$eval20.invokeStatic(core.clj:1)
    at man.core$eval20.invoke(core.clj:1)
    at clojure.lang.Compiler.eval(Compiler.java:6927)
    at clojure.lang.Compiler.eval(Compiler.java:6916)
    at clojure.lang.Compiler.load(Compiler.java:7379)
    ... 42 more

I am wondering whether this could be a problem with the order in which the namespaces get compiled. Maybe leiningen is handling man.core before the class man.ManGate gets created!?

Edit:

I could solve my problem by changing the way I implement the interface:

(ns man.gateway                                                                  
  (:import [eu.m2machine.gw Gateway]))                                             

(defn gateway []                                                                 
  (reify Gateway                                                                 
    (startup [this])                                                    
    (shutdown [this]))                                                           
  )

And in the namespace man.core I replaced the constructor call ManGate. with a call to man.gateway/gateway.

Still I would be interested to know why the solution above didn't work.


Solution

  • You class is defined in namespace man.gateway, but it's fully qualified class name is man.ManGate. Changing FQCN to man.gateway.ManGate in both core.clj and gateway.clj should solve the problem.