0

我在文件 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.

我在这里做错了什么?

4

2 回答 2

3

正如我在回答您的另一个问题时所说的那样,project.clj 文件不是放置源代码的地方-project.clj 由 leiningen 加载以设置您的项目配置,并且根本不能保证将任意代码放在那里,并且肯定会弄乱您在其中定义的名称空间的加载。遵循源库的约定并将文件放在项目树中的 src 目录下。

于 2012-01-26T17:14:42.553 回答
0

$ 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

于 2022-01-29T05:43:40.337 回答