Skip to main content
Ben Nadel at CFUNITED 2010 (Landsdown, VA) with: Virginia Klebau
Ben Nadel at CFUNITED 2010 (Landsdown, VA) with: Virginia Klebau

Listing All Classes In A Jar File Using ColdFusion And CFZip

By
Published in Comments (3)

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!

Want to use code from this post? Check out the license.

Reader Comments

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.

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

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel