Loading Java Libraries on the Fly (and Parsing RSS Easily)

Posted on May 11, 2006

Ok, I have to admit that the first time I read Mark Mandel's post, it went over my head. I was familiar with Spike's well known post for loading java libraries without adding them to the class path, but the significance of Mark's post didn't strike me until last night (with a memory refresh this morning from Paul Hastings).

See, for fun I was trying to load a java library with methods for xml parsing that includes methods for parsing RSS (yes, I know I need a life). You can load this jar using Spike's method, but only an empty instance. To actually load in an RSS feed, you need to supply a URL to the constructor, however there was no way to access the constructor (I tried several methods to no avail). Mark's method solves this issue. Here's an example.First of all, I downloaded the javaLoader.cfc that Mark recently committed within util folder of the Transfer ORM Library. Mark's component loads all the jar files in Transfer's lib directory. I tweaked it a little to add a getter and setter for allowing you to customize the directory when you initialize the component (this also required moving a few lines in the init as well), but it was a simple change.

Now, all I need to do is load the component and point it to where my mjxml.jar file is kept (for the purposes of this example, everything is in the same directory) and Shazam! I have an easy-to-use RSS parser. Well, there is a little more to it than that, so here is the code:

<cfset javaLoader = createObject("component","javaLoader").init(".") /> <cfset rssURL=createObject('java','java.net.URL')> <cfset rssURL.init("http://www.remotesynthesis.com/blog/rss.cfm?mode=short") /> <cfset rss = javaLoader.create("com.myjavatools.xml.Rss").init(rssURL)> <cfset arrItems = rss.getItems() /> <cfloop from="1" to="#arrayLen(arrItems)#" index="i">    <cfoutput><p><strong><a href="#arrItems[i].getLink()#">#arrItems[i].getTitle()#</a></strong><br />    #arrItems[i].getDescription()#    </p></cfoutput> </cfloop>

That's it! You can see an example here. As you can see, that little bit of code gave me access to quickly and easily parsing my RSS feed just by supplying the URL on initialization. Anyway, I don't know how useful this mjxml library will be in the end (like I said, it was just messing around and I am new to Java), but I intend to explore it further. However, I do know that Mark's class loader opens up a load of possibilities.


Jim Collins Mark has done some really great work here. The use of coldfusion.runtime.java.JavaProxy is very innovative. Great job!

Posted By Jim Collins / Posted on 05/11/2006 at 10:08 PM

Mark Mandel Since you all bugged me so much ;o) I wrote a generic version of the JavaLoader.cfc that takes an array of filePaths to JAR files, or directories of .class files. Details can be found at: http://www.compoundtheory.com/?action=displayPost&ID=117

Glad to see you enjoyed using the code!

Posted By Mark Mandel / Posted on 05/11/2006 at 11:51 PM

Brian Rinaldi Thanks Mark. Already added it to the open-source list. I am going to update my code using your version rather than my customized version.

Posted By Brian Rinaldi / Posted on 05/12/2006 at 7:42 AM

Write your comment

(it will not be displayed)


My name is Brian Rinaldi and I am the Web Community Manager for Flash Platform at Adobe. I am a regular blogger, speaker and author. I also founded RIA Unleashed conference in Boston. The views expressed on this site are my own & not those of my employer.