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

Calendar

Sun Mon Tue Wed Thu Fri Sat
      1
2 3 4 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       

Subscribe

Enter your email address to subscribe to this blog.

Tags

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

Recent Comments

Ajax Form Submission Revisited And Advice Needed
Hammo777 said: To solve your original problem: document.formname.onsubmit(); just call the onsubmit method yourse... [More]

Editing A Query In A SQL Server DTS Package
JD said: Thanks for your post. Never unlike Microsoft to hide stuff in the hardest part time find. [More]

Mashing Spry Effects With CF8 Ajax Goodness
Mark Pitts said: I have had moderate success implementing Spry Accordian. Sadly the part that does is not working wil... [More]

Chinese Birth Calendar Accuracy Test
Toni Lehman said: This calendar was accurate for both my daughters and 4 grandchildren. I tried it for 11 of my other ... [More]

Virtual Memory - Am I The Last To Know?
Larry Miller said: The authors friend was right. Windows virtual memory system was designed by experts and they fully u... [More]

RSS


coldfusionbloggers

FullAsAGoog MXNA

Consumed By Feed-Squirrel.com