【问题标题】:Running a JAR file created by Leiningen运行 Leiningen 创建的 JAR 文件
【发布时间】:2012-01-26 17:05:04
【问题描述】:

我在 project.clj 文件中有以下代码:

(defproject pinger "0.0.1-SNAPSHOT"
  :description "A website availability tester"
  :dependencies [[org.clojure/clojure "1.3.0"]]
  :main pinger.core)

(ns pinger.core
  (:import (java.net URL HttpURLConnection))
  (:gen-class))

(defn response-code [address]
  (let [conn ^HttpURLConnection (.openConnection (URL. address))
    code (.getResponseCode conn)]
    (when (< code 400)
      (-> conn .getInputStream .close))
    code))

(defn available? [address]
  (= 200 (response-code address)))

(defn -main []
  (let [addresses '("http://google.com"
            "http://amazon.com"
            "http://google.com/badurl")]
    (while true
      (doseq [address addresses]
    (println (available? address)))
      (Thread/sleep (* 1000 60)))))

我创建了一个 uberjar:

C:\Documents and Settings\vreinpa\My Documents\Books\ProgrammingClojure\code\src
\pinger>lein uberjar
Cleaning up.
Copying 1 file to C:\Documents and Settings\vreinpa\My Documents\Books\Programmi
ngClojure\code\src\pinger\lib
Warning: *classpath* not declared dynamic and thus is not dynamically rebindable
, but its name suggests otherwise. Please either indicate ^:dynamic *classpath*
or change the name.
Copying 1 file to C:\Documents and Settings\vreinpa\My Documents\Books\Programmi
ngClojure\code\src\pinger\lib
Created C:\Documents and Settings\vreinpa\My Documents\Books\ProgrammingClojure\
code\src\pinger/pinger-0.0.1-SNAPSHOT.jar
Including pinger-0.0.1-SNAPSHOT.jar
Including clojure-1.3.0.jar
Created C:\Documents and Settings\vreinpa\My Documents\Books\ProgrammingClojure\
code\src\pinger/pinger-0.0.1-SNAPSHOT-standalone.jar

然后我尝试运行该 uberjar 并收到以下错误:

C:\Documents and Settings\vreinpa\My Documents\Books\ProgrammingClojure\code\src
\pinger>java -jar pinger-0.0.1-SNAPSHOT-standalone.jar
Exception in thread "main" java.lang.NoClassDefFoundError: pinger/core
Caused by: java.lang.ClassNotFoundException: pinger.core
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: pinger.core. Program will exit.

我在这里做错了什么?

【问题讨论】:

    标签: clojure leiningen uberjar


    【解决方案1】:

    正如我所说的in response to your other question,project.clj 文件不是放置源代码的地方 - project.clj 由 leiningen 加载以设置您的项目配置,并且不能保证将任意代码放在那里 完全,并且肯定会搞乱您在其中定义的命名空间的加载。按照conventions for source libs 将文件放在项目树中的 src 目录下。

    【讨论】:

    • 这是“Programming Clojure”一书中的一个示例,其中隐含地告诉将代码放入project.clj。我想,作者在写这一章时并没有太努力。
    【解决方案2】:

    $ lein new pinger
    $ cd pinger
    $ lein deps

    pinger/project.clj

    (defproject pinger "0.0.1-SNAPSHOT"
      :description "A website availability tester"
      :dependencies [[org.clojure/clojure "1.3.0"]]
      :main pinger.core)
    

    pinger/src/pinger/core.clj

    (ns pinger.core
      (:import (java.net URL HttpURLConnection))
      (:gen-class))
    
    (defn response-code [address]
      (let [conn ^HttpURLConnection (.openConnection (URL. address))
        code (.getResponseCode conn)]
        (when (< code 400)
          (-> conn .getInputStream .close))
        code))
    
    (defn available? [address]
      (= 200 (response-code address)))
    
    (defn -main []
      (let 
        [addresses
          '("http://google.com"
            "http://amazon.com"
            "http://google.com/badurl")]
      (while true
        (doseq [address addresses]
          (println (available? address)))
        (Thread/sleep (* 1000 60)))))
    

    $ cd pinger
    $ lein uberjar

    绝对路径
    $ java -jar /vagrant/MyClojure/pinger/target/pinger-0.0.1-SNAPSHOT-standalone.jar

    来自 project.clj 的相对路径
    $ java -jar ./target/pinger-0.0.1-SNAPSHOT-standalone.jar

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多