Compiling Java Classes On The Fly In ColdFusion
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 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:
<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?



Sad, but this makes it look much easier.
THAT is GREAT!
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.
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.