Listing All Classes In A Jar File Using ColdFusion And CFZip

Posted October 3, 2008 at 9:25 AM

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:

 Launch code in new window » Download code as text file »

  • <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!

Download Code Snippet ZIP File

Comments (3)  |  Post Comment  |  Ask Ben  |  Permalink  |  Print Page





Reader 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.

Posted by todd sharp on Oct 3, 2008 at 9:38 AM


@Todd,

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

Posted by Ben Nadel on Oct 3, 2008 at 9:39 AM


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

Posted by Bijan Atri on Nov 18, 2008 at 10:26 AM


Post Comment  |  Ask Ben


Home   |   Web Log   |   ColdFusion   |   Projects   |   Resume   |   Job Form   |   Search   |   Contact
Epicenter Consulting - Custom Software Solutions for Business Evolution HostMySite.com - The Leader In ColdFusion Hosting