“lein jar”和“lein uberjar”没有正确设置主类

发布于 2024-09-18 13:58:53 字数 894 浏览 10 评论 0原文

我在我的项目上运行 lein uberjar 并创建了相应的 jar 文件。当我运行 jar 时,会引发 ClassNotFoundException: explodingdots.core 。我指定 explodingdot.core 作为我的主类。我解压了jar文件,对应目录下确实没有core.class。我忘记了什么?

我的 src/explodingdots/core.clj 中有以下代码,

(ns explodingdots.core  
 (:import (java.awt Color Dimension Graphics2D AlphaComposite RenderingHints)
          (java.awt.event ActionListener MouseAdapter WindowAdapter)
          (javax.swing Timer JPanel JFrame))
  (:gen-class))

[ ... ]

(defn -init[] exploding-dots)
(defn -main[_]
  (let [ed (new explodingdots.core)]
    (.init ed)))

我的 project.clj 的内容是:

(defproject explodingdots "0.1"
  :dependencies [[org.clojure/clojure "1.2.0"]
                 [org.clojure/clojure-contrib "1.2.0"]]
  :main explodingdots.core)

注意: 我正在使用 leiningen 1.3 .1

I ran lein uberjar on my project and it created the corresponding jar files. When I run the jar a ClassNotFoundException: explodingdots.core is thrown. I specified explodingdot.core as my main class. I extracted the jar file and there was indeed no core.class in the corresponding directory. What did I forget?

I have the following code in src/explodingdots/core.clj

(ns explodingdots.core  
 (:import (java.awt Color Dimension Graphics2D AlphaComposite RenderingHints)
          (java.awt.event ActionListener MouseAdapter WindowAdapter)
          (javax.swing Timer JPanel JFrame))
  (:gen-class))

[ ... ]

(defn -init[] exploding-dots)
(defn -main[_]
  (let [ed (new explodingdots.core)]
    (.init ed)))

The content of my project.clj is:

(defproject explodingdots "0.1"
  :dependencies [[org.clojure/clojure "1.2.0"]
                 [org.clojure/clojure-contrib "1.2.0"]]
  :main explodingdots.core)

Note: I am using leiningen 1.3.1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

顾冷 2024-09-25 13:58:53

好吧,我解决了原来的问题。承认这一点有点尴尬,但我想为了这篇文章的完整性我必须这么做。我与自己的道路混淆了。我在 Netbeans 项目和 leiningen 项目中有相同的文件。我正在编辑 Netbeans 文件。对不起。

但后来我遇到了另一个问题。找到了主要方法,但我得到了一个

java.lang.IllegalArgumentException: Wrong number of args (0) passed to: core$-main

“我尝试更改我的主要方法”
(defn -main [_] ...)(defn -main [& args] ...) 就像 Arthur 建议的那样,但这不起作用。为了解决这个问题,我只写了 (defn -main[]...) ,没有参数。

下一个问题是从 (main) 调用 (init) 导致错误。我通过根本不调用 (init) 而是直接从 (main) 调用 (exploding-dots) 来解决这个问题。

因此,为了使一切正常工作,我的 src/explodingdots/core.clj 看起来像

(ns explodingdots.core  
 (:import (java.awt Color Dimension Graphics2D AlphaComposite RenderingHints)
          (java.awt.event ActionListener MouseAdapter WindowAdapter)
          (javax.swing Timer JPanel JFrame))
  (:gen-class))

[ ... ]

(defn -main[] (exploding-dots))

通过查看解决方案,我必须思考,为什么我不提前写下来。这是最简单、最直接的方法。也许我需要一个假期;)。

Ok I solved my original problem. It is kind of embarassing to admit it, but I think I have to do it for the sake of completeness of this thread. I got mixed up with my paths. I have the same file within a Netbeans project and in a leiningen project. And I was editing the Netbeans file. Sorry.

But then I had an other problem. The main method is found but I get an

java.lang.IllegalArgumentException: Wrong number of args (0) passed to: core$-main

I tried changing my main method from
(defn -main [_] ...) to (defn -main [& args] ...) like Arthur suggested but that didn't work. To solve this I wrote just (defn -main[]...) with no args.

The next problem was that calling (init) from (main) resulted in an error. I worked around that by not calling (init) at all but calling (exploding-dots) directly from (main).

So to make everything work my src/explodingdots/core.clj looks like

(ns explodingdots.core  
 (:import (java.awt Color Dimension Graphics2D AlphaComposite RenderingHints)
          (java.awt.event ActionListener MouseAdapter WindowAdapter)
          (javax.swing Timer JPanel JFrame))
  (:gen-class))

[ ... ]

(defn -main[] (exploding-dots))

By looking at the solution I have to think, why didn't I write that right ahead. It is the most simple and most straight forward way. Maybe I need a vacation ;).

征棹 2024-09-25 13:58:53

我必须向主名称空间添加第三个组件,并将所有内容移至 src 下的 com 子目录中。

com.explodingdots.core

我还声明 main 接受一个 arg 向量,不确定这是否有区别:

(declare main) 
(defn -main [& args]    

I had to add a third component to my main name space and move everything into the com subdirectory under src.

com.explodingdots.core

I also declare main to take an arg vector, not sure if that makes a diference:

(declare main) 
(defn -main [& args]    
蓝戈者 2024-09-25 13:58:53

我遇到了这个问题,并通过将

:gen-class 添加到相应的缺失类中来修复它。

I ran into this, and fixed it by adding

:gen-class to the corresponding missing class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文