Iterating over an Enumeration with CFMX

June 19, 2003

I tried writing some CFML code to iterate over a java.util.Enumeration, a fairly common thing to do in java, but was plagued with java.lang.IllegalAccessException's.

IllegalAccess exceptions usually occurr when you try to call a method using reflection (which CFMX does) that doesn't exist. However I'm quite sure that hasMoreElements() is a method of java.util.Enumeration. In fact if I cfdump the propNames variable the hasMoreElements method shows up! Here's the code I'm using:

<cfset system = CreateObject("java", "java.lang.System")>
<!--- properties is a java.util.Properties object --->
<cfset properties = system.getProperties()>

<!--- propNames is a java.util.Enumeration --->
<cfset propNames = properties.propertyNames()>

 <cfloop condition="propNames.hasMoreElements()">
	<cfset propName = propNames.nextElement()>
	#propName# = #system.getProperty(propName)#<br />

I suspect the problem has something to do with CFMX not realizing that propNames is an Enumeration, I wish that JavaCast accepted more types besides the primitives.

Here's the stack trace I'm receiving, I'm using CFMX U3:

	at java.lang.reflect.Method.invoke(Native Method)
	at coldfusion.runtime.StructBean.invoke(Unknown Source)
	at coldfusion.runtime.CfJspPage._invoke(Unknown Source)
	at Statement12.evaluate(Unknown Source)
	at coldfusion.runtime.CFPage.evaluateCondition(Unknown Source)
	at cftesta2ecfm779424527.runPage(C:\web\testa.cfm:19)
	at coldfusion.runtime.CfJspPage.invoke(Unknown Source)
	at coldfusion.tagext.lang.IncludeTag.doStartTag(Unknown Source)
	at coldfusion.filter.CfincludeFilter.invoke(Unknown Source)
	at coldfusion.filter.ApplicationFilter.invoke(Unknown Source)
	at coldfusion.filter.PathFilter.invoke(Unknown Source)
	at coldfusion.filter.LicenseFilter.invoke(Unknown Source)
	at coldfusion.filter.ExceptionFilter.invoke(Unknown Source)
	at coldfusion.filter.BrowserDebugFilter.invoke(Unknown Source)
	at coldfusion.filter.ClientScopePersistenceFilter.invoke(Unknown Source)
	at coldfusion.filter.BrowserFilter.invoke(Unknown Source)
	at coldfusion.filter.GlobalsFilter.invoke(Unknown Source)
	at coldfusion.filter.DatasourceFilter.invoke(Unknown Source)
	at coldfusion.CfmServlet.service(Unknown Source)
	at jrun.servlet.ServletInvoker.invoke(
	at jrun.servlet.JRunInvokerChain.invokeNext(
	at jrun.servlet.JRunRequestDispatcher.invoke(
	at jrun.servlet.ServletEngineService.dispatch(
	at jrun.servlet.http.WebService.invokeRunnable(
	at jrunx.scheduler.ThreadPool$DownstreamMetrics.invokeRunnable(
	at jrunx.scheduler.ThreadPool$ThreadThrottle.invokeRunnable(
	at jrunx.scheduler.ThreadPool$UpstreamMetrics.invokeRunnable(

I may end up reporting this one to Macromedia as a bug, unless I'm missing something here.

2 people found this page useful, what do you think?


Hi Pete, It's a CF5 and CFMX6 bug that others have also found. I know now workaround. But you can upgrade to CFMX6.1. You might still sometimes find the bug, but iterating over props.propertyNames() and props.keys() works for me. <cfscript> ps = CreateObject("java", "java.util.Properties"); ps.setProperty("clau", "valor"); ps.setProperty("clau1", "valor1"); ps.setProperty("clau2", "valor2"); ps.setProperty("clau3", "valor3"); // funciona en CFMX6.1 keys = ps.keys(); WriteOutput("<br/>"); while (keys.hasMoreElements()){ k = keys.nextElement(); v = ps.getProperty(k); WriteOutput(k & " = " & v & "<br/>"); }//while </cfscript>
This code now works as of CFMX 6.1

Post a Comment


Spell Checker by Foundeo

Recent Entries


did you hack my cf?