java 2022-12-15

When you need to call a Clojure function from Java, is this the easiest way to do it?

IFn require = Clojure.var("clojure.core", "require");
        require.invoke(clojure.lang.Symbol.create("stats-clj.special"));
        IFn inc = Clojure.var("stats-clj.special", "gamma");
        System.out.println(inc.invoke(5));
I found part of it here https://clojure.org/reference/java_interop#_calling_clojure_from_java But it's not talking about how to actually load custom code.

Alex Miller (Clojure team) 2022-12-15T13:45:19.618919Z

This is the correct way to do it. What does ‘custom code” mean?

Alex Miller (Clojure team) 2022-12-15T13:52:10.970249Z

With above you can load any namespace and invoke any var

By "custom code" I meant simply loading something outside clojure.core, like the stats-clj.special ns above 🙂 Thanks for confirmation - perhaps it would be a good addition to the .org reference linked above?

Alex Miller (Clojure team) 2022-12-15T14:11:59.999639Z

I don’t understand what’s different that needs to be added

Alex Miller (Clojure team) 2022-12-15T14:12:41.521409Z

It says “Functions in clojure.core are automatically loaded. Other namespaces can be loaded via require:”

Oh I completely missed that example - sorry, all is good then

technically clojure.lang.Symbol.create is not part of the api

its just var and IFn

Alex Miller (Clojure team) 2022-12-16T15:02:10.139619Z

You can use Clojure.read() instead