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 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
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.
# Posted By Ben Nadel | 5/23/08 7:08 PM
Ohhhh Myyyy Goooodnessss...

THAT is GREAT!
# Posted By John Allen | 5/23/08 11:22 PM
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.
# Posted By Brian Meloche | 5/24/08 12:57 AM
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.
# Posted By Sana | 5/24/08 10:19 AM
Sure. I'm going to try and put something together over the next few days and I'll release it.
# Posted By todd sharp | 5/24/08 3:44 PM
This seems a very interesting piece of code: <cfset compiler = createObject("component", "compiler") />

I would like to see internal magic of this compiler component?
# Posted By Ardeshir Sepahsalar | 1/26/09 5:39 PM
@Ardeshir

I posted a followup here:

http://cfsilence.com/blog/client/index.cfm/2008/5/...
# Posted By todd sharp | 1/27/09 10:16 AM
What version of CF are you using? I get a "Could not find the ColdFusion Component compiler" error in CF7 Enterprise.
# Posted By Richard Davies | 2/12/09 1:55 PM
@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.
# Posted By todd sharp | 2/12/09 2:37 PM

Calendar

Sun Mon Tue Wed Thu Fri Sat
   1234
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31  

Subscribe

Enter your email address to subscribe to this blog.

Tags

actionscript ajax blogging cfsnippets coldfusion flash forms flex funny stuff javascript misc model-glue off topic personal project learn slidesix sql

Recent Comments

Chinese Birth Calendar Accuracy Test
mama to be said: ok so i will be 2 months shy of 18 when i have my baby. this calendar does not technically work for ... [More]

Fixing 'User Profile Service Failed The Logon' on Vista
Mike said: That fix worked although all i did was remove .bak and reset state to 0. User was able to log in to... [More]

Chinese Birth Calendar Accuracy Test
Melissa said: Wrong for my daughter, which it predicted to be a boy... we'll see for #2. Predicts a girl (maybe, f... [More]

Adding Auto Generated Code Downloads to BlogCFC
fweerw said: http://www.ibiblio.org/st... http://www.cambodia.ait.a...... [More]

Check Out The New SlideSix
Todd Sharp said: Thanks for the feedback Ben & Rachel! I'll keep it all in mind as I tweak things over the next ... [More]

RSS


adobe community experts

coldfusionbloggers

FullAsAGoog MXNA

Consumed By Feed-Squirrel.com