我有一个hello.clj如下.
(ns hello)
(defn hi [] (println "HI"))
Run Code Online (Sandbox Code Playgroud)
通常,我可以使用main.clj中的这个函数,如下所示.hello.clj位于包含main.clj的同一目录中.类路径包括.(当前路径).
(use 'hello)
(hi)
Run Code Online (Sandbox Code Playgroud)
我怎样才能将这个hello.clj用于'lein uberjar'?
我用'lein new myproject; lein deps'获得以下结构.
.
|-- README
|-- classes
| `-- myproject
|-- lib
| |-- clojure-1.2.0-beta1.jar
| |-- clojure-contrib-1.2.0-beta1.jar
| `-- lucene-core-3.0.2.jar
|-- project.clj
|-- src
| `-- myproject
| `-- core.clj
`-- test
`-- myproject
`-- test
`-- core.clj
project.clj如下.
(defproject myproject "1.0.0-SNAPSHOT"
:description "FIXME: write"
:dependencies [[org.clojure/clojure "1.2.0-beta1"]
[org.clojure/clojure-contrib "1.2.0-beta1"]
[org.apache.lucene/lucene-core "3.0.2"]]
:main myproject.core)
Run Code Online (Sandbox Code Playgroud)
而core.clj如下.
(ns myproject.core
(:gen-class))
(use 'hello)
(defn test1 [] (println "hello"))
(defn -main [& args]
(do
(println "Welcome to my project! These are your args:" args)
(test1)
(hi)))
Run Code Online (Sandbox Code Playgroud)
现在,我在哪里放hello.clj?我试图将它复制到myproject/src目录并运行uberjar来获取jar.但是,运行jar会导致此错误消息.
prosseek:myproject smcho$ java -jar myproject-1.0.0-SNAPSHOT-standalone.jar a d d Exception in thread "main" java.lang.ExceptionInInitializerError Caused by: java.io.FileNotFoundException: Could not locate hello__init.class or hello.clj on classpath: (core.clj:0) ...
我在这里上传了这个项目.
你把hello.clj放在src/myproject下面,所以它的ns应该是myproject.hello.有了这个文件结构,我希望hello.clj可以说(ns myproject.hello)和core.clj说(use 'myproject.hello).
当我做出这些改变时,我得到:
$ java -jar myproject-standalone.jar a b c
Welcome to my project! These are your args: (a b c)
hello
HI
Run Code Online (Sandbox Code Playgroud)