Listing All Classes In A Jar File Using ColdFusion And CFZip

Posted October 3, 2008 at 9:25 AM by Ben Nadel

Tags: ColdFusion

Last night, I made a quick post about getting all the classes available in a JAR file using ColdFusion. I went about this using a JarInputStream. Thankfully Todd Sharp and Simon Free reminded me that ColdFusion's CFZip tag can read JAR files as well as ZIP archives. Using ColdFusion's CFZip tag actually makes my GetJarClasses() much more simple:

  • <cffunction
  • name="GetJarClasses"
  • access="public"
  • returntype="array"
  • output="false"
  • hint="I return an array of classes in the given JAR file (expanded path).">
  •  
  • <!--- Define arguments. --->
  • <cfargument
  • name="JarFilePath"
  • type="string"
  • required="true"
  • hint="I am the expanded path of the JAR file."
  • />
  •  
  • <!--- Define the local scope. --->
  • <cfset var LOCAL = {} />
  •  
  • <!--- Create a default array of classes. --->
  • <cfset LOCAL.Classes = [] />
  •  
  • <!--- Get all of the .class files out of the JAR file. --->
  • <cfzip
  • action="list"
  • file="#ARGUMENTS.JarFilePath#"
  • recurse="true"
  • filter="*.class"
  • name="LOCAL.JarEntry"
  • />
  •  
  • <!---
  • Now that we have the .CLASS entries, let's loop over
  • them, clean them up, and add them to our results array.
  • --->
  • <cfloop query="LOCAL.JarEntry">
  •  
  • <!--- Clean up path structure. --->
  • <cfset LOCAL.ClassName = REReplace(
  • LOCAL.JarEntry.Name,
  • "[\\/]",
  • ".",
  • "all"
  • ) />
  •  
  • <!--- Strip off the ".class" path item. --->
  • <cfset LOCAL.ClassName = REReplaceNoCase(
  • LOCAL.ClassName,
  • "\.class$",
  • "",
  • "one"
  • ) />
  •  
  • <!--- Add the formatted class name. --->
  • <cfset ArrayAppend(
  • LOCAL.Classes,
  • LOCAL.ClassName
  • ) />
  •  
  • </cfloop>
  •  
  • <!--- Return the array of classes. --->
  • <cfreturn LOCAL.Classes />
  • </cffunction>

Sometimes you forget how easy ColdFusion makes random tasks. Thanks guys for the reminder!




Reader Comments

Oct 3, 2008 at 9:38 AM // reply »
48 Comments

Right on. The fun part about using cfzip is that if you leave it as a query object you can use QofQ to do quick searches to find a specific class (or just snoop around). If I'm really bored I'll snoop around the internal CF Jar files using that method.


Oct 3, 2008 at 9:39 AM // reply »
11,238 Comments

@Todd,

You never have to sell me on the power of queries :)


Nov 18, 2008 at 10:26 AM // reply »
1 Comments

Hello Ben,
I had to change the following lines to make it work
<!--- Define the local scope. --->
<cfset var LOCAL = {} />

<!--- Create a default array of classes. --->
<cfset LOCAL.Classes = [] />

to
<!--- Define the local scope. --->
<cfset var LOCAL =structNew() />

<!--- Create a default array of classes. --->
<cfset LOCAL.Classes = arrayNew(1) />

I am using CFMX7


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 21, 2013 at 7:46 PM
Using Plupload For Drag & Drop File Uploads In ColdFusion
No luck. At least I have uncovered the cause, URLScan 3.1. Here is what I see in the IIS log when a file is over 30mb. 2013-05-21 23:29:05 10.105.45.128 GET /plupload/assets/jquery/jquery-1.8. ... read »
May 21, 2013 at 6:12 PM
Using Plupload For Drag & Drop File Uploads In ColdFusion
Ben, I did not see you after Pete Freitag's Lockdown session at cfObjective but he said that IIS sets file size limits at 30MB by default which just happened to be the threshold for file size when ... read »
May 21, 2013 at 11:51 AM
Ask Ben: Parsing Very Large XML Documents In ColdFusion
Looking at my first ever XML document that I have to parse and put into MS SQL 2000 with CF8. I get it to list the desired Field name, many times over, and have a long list of this field name displa ... read »
May 21, 2013 at 9:25 AM
Turning Off and On Identity Column in SQL Server
you are awesome..i am lucky to get this blog between such a garbage one....Thanks, Prashant ... read »
May 20, 2013 at 4:38 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Dana, Your confusion is well founded, since this is a very confusing features. In fact, it ONLY works if you use array notation. Meaning, that this: arrayToList( query[ "columnName" ] ) ... read »
May 20, 2013 at 4:34 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
I was thinking chicken and the egg, I wouldn't have expected it to work in the valuelist going in I guess. Maybe I just need a beer, long day :) ... read »
May 20, 2013 at 4:29 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Dana, That's if you're trying to reference a specific row. In this case, we're trying to reference the entire query column as one cohesive value. So, you are correct that if you wanted to output a ... read »
May 20, 2013 at 4:24 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
I thought when you used array notation to reference queries you always had to have the row or it would throw a similar error as well? ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools