質問

I understand the general idea for importing a Java class in clojure, like this:

(import 'a.random.Class)

However, lets say I've got a file Example.java containing the 'Example' class I'd like to use, how does one go about importing something like this?

Apologies if this rather noobish has already been answered, I'd been looking around but found nothing prior to posting, and thanks in advance to anyone who can shed some light.

役に立ちましたか?

解決

In order to be able to use the class defined in Example.java, you have to compile it and place the resulting class file on the classpath.

If this Java file is a helper in a Clojure project, you can use Leiningen to automate the process:

(defproject foo "1.2.3-SNAPSHOT"
  ; ...
  :java-source-paths ["src/main/java"]
  )

Assuming Example.java lives in a subdirectory of src/main/java, Leiningen will compile it and include it on the classpath when you use it to start a REPL or prepare a jar of your project. (There's also :javac-options for specifying options to be passed to the Java compiler as a vector of strings.)

If your Java source tree is more extensive and independent of the Clojure side of the project, it might make more sense to package it as a separate Java project instead. The Clojure side would then use the Java project as a dependency.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top