如何在Clojure替补中导入本地Java课程?

发布于 2025-01-23 04:00:01 字数 1827 浏览 2 评论 0原文

有类似问题的现有答案,但是他们倾向于使用Maven,这不是我项目的选择。另外,我还没有找到任何在替补中导入的语法的具体示例,尤其是当课程是本地而不是网络中的当时。

我想将Java类导入我的Clojure项目:

public class MyLocalClass1 {
    int x;
    String y;

    public MyLocalClass1() {
        this.x = 0;
        this.y = "hello there";
    }

    public MyLocalClass1(int x, String y) {
        this.x = x;
        this.y = y;
    }

    public void setX(int x) {
        this.x = x;
    }

    public void setY(String y) {
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public String getY() {
        return y;
    }
}

我成功地以Java2import.jar的想法构建了它。

这是project.clj,我在哪里导入罐子:

  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
            :url "https://www.eclipse.org/legal/epl-2.0/"}
  :dependencies [[org.clojure/clojure "1.10.1"]]
  :resource-paths ["/path/to/my/Java/project/Java2Import/artifacts/Java2Import/"]
  :repl-options {:init-ns import-local-java-4.core})

但是,当我尝试导入时,我会遇到错误:

import-local-java-4.core=> (import (Java2Import.Java2Import))
Syntax error macroexpanding clojure.core/import at (/tmp/form-init9909516591129619328.clj:1:1).
Java2Import.Java2Import - failed: #{(quote quote)} at: [:class :quoted-spec :quote] spec: :clojure.core.specs.alpha/quotable-import-list
() - failed: Insufficient input at: [:package-list :spec :classes] spec: :clojure.core.specs.alpha/package-list
Java2Import.Java2Import - failed: #{(quote quote)} at: [:package-list :quoted-spec :quote] spec: :clojure.core.specs.alpha/quotable-import-list
(Java2Import.Java2Import) - failed: simple-symbol? at: [:class :spec] spec: :clojure.core.specs.alpha/quotable-import-list

我在做什么错?

There are existing answers to similar questions, but they tend to use Maven, which is not an option for my project. Also, I have not found any which give concrete examples of the syntax you use to import at the repl, especially when the class is local as opposed to from the web.

I want to import a Java class into my Clojure project:

public class MyLocalClass1 {
    int x;
    String y;

    public MyLocalClass1() {
        this.x = 0;
        this.y = "hello there";
    }

    public MyLocalClass1(int x, String y) {
        this.x = x;
        this.y = y;
    }

    public void setX(int x) {
        this.x = x;
    }

    public void setY(String y) {
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public String getY() {
        return y;
    }
}

I successfully built it in Idea as Java2Import.jar.

Here is project.clj, where I import the jar:

  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
            :url "https://www.eclipse.org/legal/epl-2.0/"}
  :dependencies [[org.clojure/clojure "1.10.1"]]
  :resource-paths ["/path/to/my/Java/project/Java2Import/artifacts/Java2Import/"]
  :repl-options {:init-ns import-local-java-4.core})

However, when I try to import, I get errors:

import-local-java-4.core=> (import (Java2Import.Java2Import))
Syntax error macroexpanding clojure.core/import at (/tmp/form-init9909516591129619328.clj:1:1).
Java2Import.Java2Import - failed: #{(quote quote)} at: [:class :quoted-spec :quote] spec: :clojure.core.specs.alpha/quotable-import-list
() - failed: Insufficient input at: [:package-list :spec :classes] spec: :clojure.core.specs.alpha/package-list
Java2Import.Java2Import - failed: #{(quote quote)} at: [:package-list :quoted-spec :quote] spec: :clojure.core.specs.alpha/quotable-import-list
(Java2Import.Java2Import) - failed: simple-symbol? at: [:class :spec] spec: :clojure.core.specs.alpha/quotable-import-list

What am I doing wrong?

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

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

发布评论

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

评论(1

别挽留 2025-01-30 04:00:01

克隆此回购并查看目录结构,还project.clj

源代码文件看起来像:


~/io-tupelo/clj-java-template > ls -1 **/*.{clj,java}
 project.clj
 src/clj/demo/core.clj
 src/java/demo/Calc.java
 test/clj/_bootstrap.clj
 test/clj/tst/demo/core.clj

测试名称空间显示正确的语法:

(ns tst.demo.core
  (:use demo.core tupelo.core tupelo.test)
  (:require
    [tupelo.string :as str])
  (:import [demo Calc]))

(dotest
  (is= 5 (add2 2 3))            ; from src/clj/demo/core.clj
  (is= 42 (Calc/add2 29 13))    ; from src/java/demo/Calc.java
  )

test/clj/ TST/DEMO/CORE.CLJ


~/io-tupelo/clj-java-template > lein clean ; lein test
Compiling 1 source files to /Users/alanthompson/io-tupelo/clj-java-template/target/default+test+test/class-files

lein test _bootstrap

-----------------------------------
   Clojure 1.10.3    Java 17.0.2
-----------------------------------

lein test tst.demo.core

Ran 2 tests containing 2 assertions.
0 failures, 0 errors.

您无需将Java代码编译到JAR文件中。从表面上看,只需使用Java源文件,就容易得多。

您还可以在repled中运行:


~/io-tupelo/clj-java-template > lein repl
Compiling 1 source files to /Users/alanthompson/io-tupelo/clj-java-template/target/default/class-files

demo.core=> (import '[demo Calc])
demo.Calc
demo.core=> (prn :result (Calc/add2 6 7))
:result 13
nil

请注意,的函数调用语法需要import 在repl 中与(NS ...)表格!

另外,在REPL中不会拾取对Java代码的任何编辑。您将需要退出并重新启动重新编译。


ps

正如Readme所解释的那样,我发现使用Lein-Test-Refresh插件更好。恕我直言,这就像对类固醇的补充!

pps

如果您只有一个JAR文件(即Java源代码),则可能希望使用lein install命令,该命令将将JAR文件复制到本地Maven中缓存〜/.m2 Leiningen可以正常找到它。

Clone this repo and look at the directory structure and also project.clj

The source code files look like so:


~/io-tupelo/clj-java-template > ls -1 **/*.{clj,java}
 project.clj
 src/clj/demo/core.clj
 src/java/demo/Calc.java
 test/clj/_bootstrap.clj
 test/clj/tst/demo/core.clj

The test namespace shows the correct syntax:

(ns tst.demo.core
  (:use demo.core tupelo.core tupelo.test)
  (:require
    [tupelo.string :as str])
  (:import [demo Calc]))

(dotest
  (is= 5 (add2 2 3))            ; from src/clj/demo/core.clj
  (is= 42 (Calc/add2 29 13))    ; from src/java/demo/Calc.java
  )

Run the unit tests in test/clj/tst/demo/core.clj:


~/io-tupelo/clj-java-template > lein clean ; lein test
Compiling 1 source files to /Users/alanthompson/io-tupelo/clj-java-template/target/default+test+test/class-files

lein test _bootstrap

-----------------------------------
   Clojure 1.10.3    Java 17.0.2
-----------------------------------

lein test tst.demo.core

Ran 2 tests containing 2 assertions.
0 failures, 0 errors.

You don't need to compile the Java code into a JAR file. In face, it is much easier if you just use the Java source files.

You can also run in the REPL:


~/io-tupelo/clj-java-template > lein repl
Compiling 1 source files to /Users/alanthompson/io-tupelo/clj-java-template/target/default/class-files

demo.core=> (import '[demo Calc])
demo.Calc
demo.core=> (prn :result (Calc/add2 6 7))
:result 13
nil

Please note that the function call syntax of require and import is different in the REPL than the macro syntax of the (ns ...) form!

Also, any edits to your Java code will not be picked up in the REPL. You will need to exit and restart the REPL to force a Java recompile.


P.S.

As the README explains, I find it even better to use the lein-test-refresh plugin. IMHO it is like a REPL on steroids!

P.P.S.

If you only have a JAR file (i.e. not Java source code), you may wish to use the lein install command, which will copy the JAR file into the local Maven cache ~/.m2 where Leiningen can find it as normal.

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