代码之家  ›  专栏  ›  技术社区  ›  frank

Clojure lein uberjar:java。lang.ClassNotFoundException

  •  0
  • frank  · 技术社区  · 8 年前

    我有一个Clojure图书馆,有两个 gen-class 指令。当我跑步时 lein run ,没有问题。然而,当我跑步时 lein uberjar ,我得到错误:

    $ lein uberjar
    Compiling 6 source files to /Users/frank/src/user/target/uberjar/classes
    Compiling user.common
    Compiling user.core
    java.lang.ClassNotFoundException: user.server.UserAuthenticationServer, compiling:(user/core.clj:15:30)
    Exception in thread "main" java.lang.ClassNotFoundException: user.server.UserAuthenticationServer, compiling:(user/core.clj:15:30)
      at clojure.lang.Compiler.analyzeSeq(Compiler.java:6926)
    .....
      at clojure.lang.Compiler.analyze(Compiler.java:6701)
    Caused by: java.lang.ClassNotFoundException: user.server.UserAuthenticationServer
      at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    ...
      at clojure.lang.Compiler.analyzeSeq(Compiler.java:6919)
      ... 86 more
    

    除了生成的java文件外,还有 project.clj , server.clj 和 core.clj .

    (defproject user "0.1.0-SNAPSHOT"
      :dependencies [[org.clojure/clojure                        "1.9.0-alpha14"]
                     [io.grpc/grpc-core                          "1.7.0"]
                     [io.grpc/grpc-netty                         "1.7.0"
                      :exclusions [io.grpc/grpc-core]]
                     [io.grpc/grpc-protobuf                      "1.7.0"]
                     [io.grpc/grpc-stub                          "1.7.0"]]
      :main ^:skip-aot user.core
      :aot [user.server]
      :target-path "target/%s"
      :source-paths ["src/clj"]
      :java-source-paths ["src/generated/proto"
                          "src/generated/grpc"]
      :profiles {:uberjar {:aot :all}})
    

    果心clj公司

    (ns user.core
      (:import [io.grpc Server ServerBuilder])
      (:gen-class))
    
    (defonce start-server-atom   (atom nil))
    (def port                    8080)
    
    (defn start-server []
      (when-not @start-server-atom
        (reset! start-server-atom
                (-> (ServerBuilder/forPort port)
                    (.addService (new user.server.UserAuthenticationServer))
                    .build
                    .start
                    .awaitTermination))))
    
    (defn -main
      [& args]
      (start-server))
    

    服务器clj公司

    (ns user.server
      (:gen-class
       :main false
       :name user.server.UserAuthenticationServer
       :extends xyz.skroo.user.UserAuthenticationGrpc$UserAuthenticationImplBase))
    
    (defn -startUserAuthentication [this req res]
      (.onNext res req)
      (.onCompleted res))
    

    这很奇怪,因为这是可行的,我认为编译时间顺序改变了,现在我无法生成一个独立的jar。

    1 回复  |  直到 8 年前
        1
  •  3
  •   buræquete Naveen Kocherla    7 年前

    :profiles{:uberjar{:aot:all}}意味着当运行uberjar时,它将尝试编译所有名称空间。当您运行lein时,它只编译:aot键中的名称空间。

    尝试将uberjar配置文件更新为仅aot服务器名称空间,看看是否有效。

    推荐文章