Company Blog RSS Feed

Listing .class files in a .jar file in ColdFusion using CFZIP

March 6, 2011
A few days ago, I came across the need for a spell checker in a customer service application I was writing. To accomplish this, I downloaded Google's Spell Checker API which contained a single .jar file. I highly recommend using their API if you ever need a spell checker in one of your applications. It's quick and easy to setup and a very powerful tool.

Before you can crack into the .jar file and start using its' classes, you'll have to upload it to your ColdFusion servers' LIB directory and restart your ColdFusion service. In my case, I uploaded the .jar file to the following location:

C:\ColdFusion\JRun4\lib


You could also upload the .jar file to any location on your server you want and setup a new class path under the ColdFusion Administrator. To do this, log into your CFAdmin, then go to Server Settings > Java and JVM and add your new location to your list of paths. Once you have completed one of these steps, restart your ColdFusion service for it to work.

Now we can dig into the .jar file and see what it contains. Set up a new array that you will use shortly to hold your class names:

<!--- create an array to hold the class names --->
<cfset arrayClasses = []>


Next, we'll use the CFZIP tag to pull the contents from our new .jar file:

<!--- get .class files from .jar file --->
<cfzip action="list" file="[your .jar file location]\google-api-spelling-java-1.1.jar"
        name="qryClasses" filter="*.class" recurse="true" />


In the above code snippet, we are asking ColdFusion to grab the google-api-spelling-java-1.1.jar file and store its' contents in a variable called qryClasses. We are asking it to only pull .class files using the filter attribute and asking it to pull all files from all subdirectories by setting recurse to "true".

You can dump the contents of the qryClasses variable if you'd like to see what information is being pulled from your .jar file. It contains useful information such as the file names, dates they were last modified, and actual and compressed sizes of your files. In this case, we are looking for the file names only, but need to make sure they are in a format we can use:

<!--- loop through .jar contents, parse class names, and append to array --->
<cfloop query="qryClasses">
    <cfset ClassName = Replace(Name, "/", ".", "all")>
    <cfset ClassName = ReplaceNoCase(ClassName, ".class", "")>
    
    <cfset ArrayAppend(arrayClasses, ClassName)>
</cfloop>


In short, what we're doing here is looping through our qryClasses query that contains all the data from our .jar file, replacing any front slash (/) characters with a period (.) since ColdFusion uses dot notation for component locations, and storing our new class names in the arrayClasses array we created in the beginning.

Once this is done, dump your array contents to the screen to take a look at the classes we can use from the Google Spell Checker API:

<!--- dump the array contents --->
<cfdump var="#arrayClasses#">


That's all there is to it! Pretty soon here, I'll write up a blog on how to dig even deeper into the classes and start taking advantage of its' functionality! For now, visit Google's page for further explanation of their API's usage and to download the .jar file. You can find it at:

<a href="http://code.google.com/p/google-api-spelling-java/" target="_blank">http://code.google.com/p/google-api-spelling-java/<;/a>




Reader Comments



blackjack online
October 24, 2011
The program says that the code error
<!--- get .class files from .jar file --->
<cfzip action="list" file="[your .jar file location]\google-api-spelling-java-1.1.jar"
        name="qryClasses" filter="*.class" recurse="true" />



Add A Comment





Subscribe to this blog