简体   繁体   English

如何在Clojure REPL导入本地Java class?

[英]How to import local Java class at Clojure REPL?

There are existing answers to similar questions, but they tend to use Maven, which is not an option for my project.类似问题已有答案,但他们倾向于使用 Maven,这不是我的项目的选项。 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.此外,我还没有找到任何具体示例来说明您用于在 repl 中导入的语法,尤其是当 class 是本地的而不是来自 web 时。

I want to import a Java class into my Clojure project:我想将 Java class 导入到我的 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;
    }
}

I successfully built it in Idea as Java2Import.jar.我在 Idea 中成功将其构建为 Java2Import.jar。

Here is project.clj, where I import the jar:这是 project.clj,我在其中导入 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?我究竟做错了什么?

Clone this repo and look at the directory structure and also project.clj克隆这个 repo 并查看目录结构和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 :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.您不需要将 Java 代码编译成 JAR 文件。 In face, it is much easier if you just use the Java source files.从表面上看,如果只使用 Java 源文件就容易多了。

You can also run in the REPL:你也可以在 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!请注意, REPLrequireimport的 function 调用语法与(ns...)形式的宏语法不同!

Also, any edits to your Java code will not be picked up in the REPL.此外,对您的 Java 代码所做的任何编辑都不会在 REPL 中体现。 You will need to exit and restart the REPL to force a Java recompile.您将需要退出并重新启动 REPL 以强制进行 Java 重新编译。


PS聚苯乙烯

As the README explains, I find it even better to use the lein-test-refresh plugin.正如自述文件所解释的那样,我发现使用lein-test-refresh插件更好。 IMHO it is like a REPL on steroids!恕我直言,它就像是类固醇的 REPL!

PPS聚苯硫醚

If you only have a JAR file (ie 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.如果你只有一个 JAR 文件(即不是 Java 源代码),你可能希望使用lein install命令,它将 JAR 文件复制到本地 Maven 缓存~/.m2中,Leiningen 可以正常找到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM