Calling java functions from Clojure

As the earlier poster noted, the two examples you give are just a little off:

(.wait (java.lang.Object.) 3) ;; this actually throws an IllegalMonitorStateException

(.println java.lang.System/out "hi")

Should work!


You can use all Java functions from Clojure. See the great page on Clojure's Java interop.

In particular, you just need to get the syntax right depending on exectly what sort of Java construct you are dealing with, e.g. executing the println method on the static member "out" from java.lang.System:

(.println (System/out) "hi")

At first, Object.wait() function is not a static function, you should use as:

(.wait (java.lang.Object.) 3)

Second, Object.wait() function should be called after you get the lock. Otherwise, it will throw IllegalMonitorStateException.

Tags:

Java

Clojure