Import local Java classes into clojure

I understand the general idea of importing Java classes in clojure, as follows:

(import 'a.random.Class)

However, suppose I have a file that contains the 'example' class I want to use Java, how to import such things?

Sorry, if this rather Noobish has been answered, I've been looking around, but I got nothing before the release, and thank anyone who can solve the problem in advance

Solution

To be able to use example Java, you must compile it and put the generated class file on the class path

If this java file is a helper in the clojure project, you can use leiningen to automate the process:

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

Suppose example Java is located in the subdirectory of Src / main / Java. When you use it to start repl or prepare the jar of the project, leiningen will compile it and include it in the classpath (also: javac options, which is used to specify the options passed to the java compiler as a string vector.)

If your Java source tree is broader and independent of the clojure side of the project, it may make more sense to package it as a separate java project Clojure will then use the Java project as a dependency

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>