kandi X-RAY | clojure-from-java Summary
kandi X-RAY | clojure-from-java Summary
There are many ways to call Clojure from Java
Top functions reviewed by kandi - BETA
- Entry point for testing
- Call a native library
clojure-from-java Key Features
clojure-from-java Examples and Code Snippets
Trending Discussions on clojure-from-java
I am trying to call some Clojure code from Java, but I get this error when trying to "require" the file:...
ANSWERAnswered 2021-May-08 at 15:49
Here is a full minimal working version showing Clojure called from Java.
I'm very new to all things JVM and want to start a Java project that involves a Clojure library as a dependency. I've seen this question on how to run Clojure code from Java, but when I try to run the jar file after
mvn package, I get
cannot find symbol for
variable Clojure. My code looks like this so far:
ANSWERAnswered 2020-Jul-24 at 21:22
I have a working demo for you using
lein to build. For the Maven part, the example project at the end.
Something is puzzling me after reading the this great answer to a related question:
There are two possibilities to share a function that I wrote in Clojure with Java developers
The first one is providing it in a JAR file so that they can call it as if I had written it in Java. Sounds great for Clojure advocacy.
The second one, the purportedly better way, requires those Java developers to use things like
clojure.lang.RTand invoking functions by passing their names as strings (!) instead of just calling them.
Why is the second approach "the better one"?...
ANSWERAnswered 2020-May-31 at 07:03
You are sorta setting up a false dichotomy here. Every approach involves creating a jar file: that is just how JVM programs are distributed. But there are 3 different ways for Java code to invoke Clojure code contained in a jar:
- Use methods in clojure.lang.RT to initialize the runtime, load files, and then look up vars. This is the old, deprecated approach.
- Use methods in clojure.java.api.Clojure to look up functions and invoke them. This is the newer version of (1), and hides some of the messy stuff you could accidentally get wrong.
- Use gen-class in the Clojure library to define a more Java-friendly interface to the Clojure functions.
You can still do (3) - there's nothing wrong with it exactly. But gen-class is a pretty clunky tool, and except for the simplest examples like exposing a number of static methods, it's just not a lot of fun, and it's not easy to provide an API that "feels" like a Java API using Clojure.
But you know what's great at providing an API that feels like Java? Java! So what I recommend if you want to make a Clojure library easy to use in Java is to include some Java code in your Clojure library. That Java code, written by you, bridges the language gap. It accesses your Clojure code by mechanism (2) above, and presents a Java-friendly facade so the outside world doesn't have to know there's Clojure underneath.
amalloy/thrift-gen is an example of a library I wrote years ago following this approach. It would not be at all easy to write this in pure Clojure, just because traditional Java idioms are very foreign to Clojure, and it doesn't support them all very well. By writing my own Java shim instead, Java clients get a very comfortable interface to work with, and I can just write Clojure that feels like Clojure instead of a bunch of gen-class nonsense.
No vulnerabilities reported
You can use clojure-from-java like any standard Java library. Please include the the jar files in your classpath. You can also use any IDE and you can run and debug the clojure-from-java component as you would do with any other Java program. Best practice is to use a build tool that supports dependency management such as Maven or Gradle. For Maven installation, please refer maven.apache.org. For Gradle installation, please refer gradle.org .
Reuse Trending Solutions
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page