While helping a colleague out with a tricky problem accessing Excel files, I downloaded the Java Excel API library from SourceForge. After a quick scan of the documentation, I started to write some ColdFusion code.
<cfscript>
jxlWorkbook = createObject('java','jxl.Workbook');
file = createObject('java','java.io.File').init("C:\\temp\\test.xls");
excelFile = jxlWorkbook.getWorkbook(file);
</cfscript>
I’m immediately alerted with the message
The selected method getWorkbook was not found.
Either there are no methods with the specified method name and argument types,
or the method getWorkbook is overloaded with arguments types that ColdFusion can't
decipher reliably. If this is a Java object and you verified that the method exists,
you may need to use the javacast function to reduce ambiguity.
I dump out the object, and there are actually 5 method signatures for getWorkbook() and the one I want accepts a single argument of type java.io.File. I also know that the JavaCast() function certainly can’t handle a complex type like java.io.File. What’s a poor coder to do? I thought this was the end of the line, but figured I’d try something anyway…
<cfscript>
jxlWorkbook = createObject('java','jxl.Workbook');
excelFile = jxlWorkbook.getWorkbook(createObject('java','java.io.File').init("C:\\temp\\test.xls"));
</cfscript>
Lo, and behold, that worked perfectly! I never knew that you could use createObject() inside a method call.
There was another snag that I’d never ran across before: another method of the library wouldn’t accept integers passed from ColdFusion. In a simple from-to loop producing an index, the value of the index wasn’t being accepted by the method.
<cfloop from="0" to="#transactions.getRows() - 1#" index="row">
<cfloop from="0" to="#transactions.getColumns() - 1#" index="col">
<cfset currentCell = transactions.getCell(col, row)>
<cfoutput>#currentCell.getContents()#</cfoutput>
</cfloop>
<br/>
</cfloop>
I had to use JavaCast() to force them into the native int type.
<cfset currentCell = transactions.getCell(JavaCast("int", col), JavaCast("int", row))>
4 Responses for "Using overloaded Java methods via ColdFusion"
[...] be to consider ColdFusion a legacy language? I noticed a trackback to one of my posts about using overloaded Java methods when calling them via ColdFusion. I visited it just see what they had to say, and although it just turned out to be a aggregator [...]
Thanks man!
I spent a whole day to understand what the error.
Good Luck
I saw this error when i was passing a variable in the URL which was not required because we have a method example “getWorkbook()” which gets it for us. So, just not passing the variable solved it.
I do not know if it’s just me or if perhaps everybody else encountering issues with your website. It appears like some of the written text in your posts are running off the screen. Can someone else please comment and let me know if this is happening to them too? This might be a problem with my browser because I’ve
had this happen previously. Thanks
Leave a reply