Compiling Java Classes On The Fly In ColdFusion

Posted By : todd sharp Posted At : May 23, 2008 4:39 PM Posted In: Java, ColdFusion

10

I'm sure I'm not the first to ever figure this out, but through a bit of tapping into some underlying Java in CF I devised a method yesterday to compile Java into usable classes on the fly.

Given a directory called 'src' that contains one or more '.java' files I call:

<cfset compiler = createObject("component", "compiler") />
<cfset src = expandPath("src") />
<cfset compiled = compiler.compileJava(src) />

Which returns an array of structures. Each element contains feedback on the individual files processed. So for three java files I'd get the following if I dumped the result:

Now what is really cool about this is that by default I drop the compiled '.class' files into 'WEB-INF/classes' and they are immediately available to be created and invoked by CF. So without restarting CF I do:

<cfset e = createObject("java", "Employee").init("Todd", "Sharp", "100.0", "3") />
<cfdump var="#e#">

And I get:

Pretty cool, but I've yet to determine if there is really a good use case for this method. I do have one idea that I'm going to try if I get some time this weekend, but other then that it seems like not much more then a novelty to me. Also, since I'm pretty naive at all things Java I've not yet figured out a way (if there is one) to create packages. I tried using cfzip to jar up a few classes, drop them in the CF class path and restarting CF, but it didn't seem to work. Anyone have any ideas or thoughts on use cases here, or is this just another "Wow, that's cool" thing?

Comments (10)

Ben Nadel's Gravatar Todd, this is very cool! I have often times wanted to play around with some Java stuff, but did not since I have no idea how to actually compile :)

Sad, but this makes it look much easier.

John Allen's Gravatar Ohhhh Myyyy Goooodnessss...

THAT is GREAT!

Brian Meloche's Gravatar I am guessing you're probably not the first, but oh, man... this could be something you've stumbled upon.

I'm very curious to see if there would be any speed improvements in doing this method. I am guessing yes. If yes, this might be the magic bullet for speeding up object creation in ColdFusion. If not... it's cool, but not terribly useful.

Sana's Gravatar I think its really cool.
I can see the potential in various ways like groovy + grail-hibernate integration could be possible if runtime compile source files. is it possible to have a quick look of compiler component.

todd sharp's Gravatar Sure. I'm going to try and put something together over the next few days and I'll release it.

Ardeshir Sepahsalar's Gravatar This seems a very interesting piece of code: <cfset compiler = createObject("component", "compiler") />

I would like to see internal magic of this compiler component?

todd sharp's Gravatar @Ardeshir

I posted a followup here:

http://cfsilence.com/blog/client/index.cfm/2008/5/...

Richard Davies's Gravatar What version of CF are you using? I get a "Could not find the ColdFusion Component compiler" error in CF7 Enterprise.

todd sharp's Gravatar @Richard - compiler was my CFC that I was playing with (I created the CFC). I never did end up releasing the code, but I'll post it up tonight (it's on another machine that I don't have access to at the moment).

Please remind me if you don't see a followup comment telling you where to download by tomorrow.

Don Coughlan's Gravatar Hey there, I am eager to get a look at the compiler.cfc

Is there a chance i can get a look at it