Clojure – leiningen: about classnotfoundexception of classes appearing in “lein run” / “lein uberjar” project

I am trying to implement the java interface required by the 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 class cannot be found Both namespaces are defined in the same leiningen project, so I want them to see each other and the classes they generate

(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 attempts 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])

So far, the only two methods required for the interface are stub implementations I can get their code after compiling the code Define interfaces (in artifacts added as dependencies):

package eu.m2machine.gw;                                                         

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

The exceptions I get are:

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 wonder if this may be a matter of namespace compilation order Maybe leiningen dealt with man before class core. Mangate was created!?

Edit:

I can 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]))                                                           
  )

In namespace man In core, I replaced the constructor to call mangate Call man gateway / gateway.

I'm still interested to know why the above solution doesn't work

Solution

Your class is in the namespace man Gateway, but its fully qualified class name is man ManGate. Add core Clj and gateway Fqcn in clj is changed to man gateway. Mangate should be able to solve the problem

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>