jQuery in Action, 2nd edition*
The moose likes Clojure and the fly likes Clojure/Java Interop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Languages » Clojure
Bookmark "Clojure/Java Interop "Hello World"" Watch "Clojure/Java Interop "Hello World"" New topic
Author

Clojure/Java Interop "Hello World"

Sean Crotty
Greenhorn

Joined: Feb 10, 2011
Posts: 2
The best chance I have of getting Clojure into my company is to introduce it via small helper APIs that can be called by regular Java code. For example, I imagine tree/node manipulation would be much simpler to code in Clojure. But it would only be useful if POJ (plain ol' Java) could interact with it to perform CRUD.

Is there a good document that shows how to:
1) Write Clojure code that can be accessed by POJ?
2) Access that code via POJ?

In other words, is there a "Hello World" for Clojure/Java interop.

Thanks!
Sean
Sean Corfield
Ranch Hand

Joined: Feb 09, 2011
Posts: 261
    
    5

There's a Hello World example here:

http://clojure.org/compilation

To create functions callable from Java you need to remember that the first argument is 'this' (which is implicitly passed to Java methods).

Hopefully someone can point you to more specific Java interop examples but once you have your first function working, it's pretty much plain sailing from then on (in my experience).
Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3479

Show them simple yet important examples like how to iterate over a data structures in Clojure, this will make them cry immediately as they will recall how million times they wrote for loops in Java.
You can also solve the sleeping barber shop algorithm in Clojure to show them how elegant the concurrency programming in Clojure really is.
Sean Crotty
Greenhorn

Joined: Feb 10, 2011
Posts: 2
Sean Corfield wrote:There's a Hello World example here:

http://clojure.org/compilation

To create functions callable from Java you need to remember that the first argument is 'this' (which is implicitly passed to Java methods).

Hopefully someone can point you to more specific Java interop examples but once you have your first function working, it's pretty much plain sailing from then on (in my experience).

Thanks, Sean. That is a very helpful link!
 
 
subject: Clojure/Java Interop "Hello World"