How to call Clojure from Java?

925 views
Skip to first unread message

thelmuth

unread,
Jun 1, 2017, 9:09:41 PM6/1/17
to Clojure
What is the best way to call a Clojure function from Java?

Most of the resources I have found are either very old or don't go into enough detail. I am a Clojure programmer with very little Java background, and have been having trouble with the methods I've found, especially classpath issues.

James Reeves

unread,
Jun 1, 2017, 9:24:44 PM6/1/17
to clo...@googlegroups.com
There's a brief section on this on the Clojure website:


On 2 June 2017 at 01:35, thelmuth <trh...@gmail.com> wrote:
What is the best way to call a Clojure function from Java?

Most of the resources I have found are either very old or don't go into enough detail. I am a Clojure programmer with very little Java background, and have been having trouble with the methods I've found, especially classpath issues.

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
James Reeves

Daniel Compton

unread,
Jun 1, 2017, 10:11:25 PM6/1/17
to clo...@googlegroups.com
Hi thelmuth

Here's a fully worked example. It calls the Clojure function myco.dashed-namespace.app.core/start!. Save it to src/myco/dashed_namespace/app/Main.java.

package myco.dashed_namespace.app;

import clojure.java.api.Clojure;
import clojure.lang.IFn;

public class Main {
public static void main(String[] args) {
try {
IFn require = Clojure.var("clojure.core", "require");
require.invoke(Clojure.read("myco.dashed-namespace.app.core"));

Clojure.var("myco.dashed-namespace.app.core", "start!").invoke();
}
catch (Throwable e) {
System.out.println(e.getMessage());
}
}
}

On Fri, Jun 2, 2017 at 1:24 PM James Reeves <ja...@booleanknot.com> wrote:
There's a brief section on this on the Clojure website:

On 2 June 2017 at 01:35, thelmuth <trh...@gmail.com> wrote:
What is the best way to call a Clojure function from Java?

Most of the resources I have found are either very old or don't go into enough detail. I am a Clojure programmer with very little Java background, and have been having trouble with the methods I've found, especially classpath issues.

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to

For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
James Reeves

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to

For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.

Mark Bastian

unread,
Jun 4, 2017, 9:56:11 PM6/4/17
to Clojure
I recently put together a project that demonstrates a few ways to call Clojure from Java:

https://github.com/markbastian/java-calls-clojure

Hope that helps.

-Mark

thelmuth

unread,
Jun 24, 2017, 3:05:55 PM6/24/17
to Clojure
Thanks for the suggestions! Daniel's code helped me a lot, thanks!

One thing that had me stuck for a while is that you have to have a Clojure jar in your classpath. I also worked through a different way, using an uberjar of a Clojure project. I posted my guides here for anyone else trying to do this.

Didier

unread,
Jun 26, 2017, 10:04:58 PM6/26/17
to Clojure
Using gen-class or the Clojure Java API like Daniel showed are the best ways I know of. If you want static types in Java and auto-complete, and things to feel like Java, use gen-class, if not, use the Clojure Java API.
Reply all
Reply to author
Forward
0 new messages