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

Post Comment  |  Ask Ben  |  Print Page





Reader Comments

Oct 3, 2008 at 9:38 AM // reply »
45 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 »
7,572 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 Comment  |  Ask Ben

Recent Blog Comments
Mar 21, 2010 at 8:57 PM
The Bourne Ultimatum Starring Matt Damon And Julia Stiles
late to the party, but my observation is this: rewatch carefully for the platonic nature of the relationship between nicki and jason. she never flirts with him. he never comes on to her. they alway ... read »
Mar 21, 2010 at 7:40 PM
Is Simulating User-Input Events With jQuery Ever A Good Idea?
A couple of things. One you embed the initial state of of more-info in the CSS. IMHO, that behavior should be in jQuery: moreInfo.hide(); It shows that the behavior your toggling and closing is mor ... read »
Mar 21, 2010 at 3:59 PM
Exploring ColdFusion Component Runtime Class Properties And Serialization
@Elliott, according to Ben's experiment, serializeJSON() doesn't access the private data by default - it doesn't even access the getHair() method - so trying to clone a Girl.cfc via serializeJSON/des ... read »
Mar 21, 2010 at 3:49 PM
Ask Ben: Javascript String Replace Method
I'm confused a bit by what you are asking, but if had this sentence: The color, red, is in the style statement; style: red;. and wanted to remove all or change all of the commas, colons, and semi-c ... read »
Mar 21, 2010 at 3:13 PM
Ask Ben: Javascript String Replace Method
I am trying to make a java program to count the number of times that these punctuation marks occur in a body of text: , : ; . ! - ' " ? / \ I am using this piece to ferret out the commas: numcommas ... read »
Mar 21, 2010 at 11:13 AM
A New Wrist Pain
@chiropractor suwanee, Spoken like someone trying to sell something. Other than for minor, temporary relief from some back pain, chiropractic treatment is nothing but placebo effect and quackery. ... read »
Mar 21, 2010 at 6:32 AM
ColdFusion CFPOP - My First Look
Apologies... The field name in the db for C. is "BounceCode" It stores the code / message which is returned in the email. Sorry for the confusion. ... read »
Mar 21, 2010 at 6:29 AM
ColdFusion CFPOP - My First Look
@Jose Galdamez, Hi Ben and Jose 1st of all.. big thanks to Jose for his Skype chat a few weeks back. Your time was much appreciated. I have come up with a rather unelegant solution to my problem a ... read »