JVM not finding coldfusion classes

EDIT: Sorry, for context: I’m running commandbox cfusion 2018 and succesfully compiled this java file and created the object in a cfcomponent.

I created a java class, a simple utility for converting queries to arrays to structs. I may have some bad logic towards the end (I’m on the java learning curve) but I can’t for the life of me figure out why it can’t find this method when it runs. With Java, my understanding is that not finding a method can be because of a mismatch in parameters, but if I change the input parameter to an Object and return getClass().getName() it returns coldfusion.sql.QueryTable. So I’m not sure what’s going on here… I tried adding the cfusion folder to libdirs (feels redundant) but it did not solve the problem. Can someone help me out here? Code below:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import coldfusion.sql.QueryTable;

public class QueryToArray {
public List<Map<String, Object>> run(QueryTable query) {

    // Response is Array of Structs
    List<Map<String, Object>> result = new ArrayList<>();

    // Data rows and columns
    String[] columnNames = (String[]) query.getColumnList();
    Integer recordCount = query.getRecordCount();

    // Create arrays
    for (int row = 1; row <= recordCount; row ++) {
        Map<String, Object> rowMap = new HashMap<>();

        for (int i = 0; i < columnNames.length; i++) {
            rowMap.put(columnNames[i], query.getField(row, i+1));
        }

        result.add(rowMap);
    }

    return result;

} 

}

I’m not sure what the use case is here since you could use the qb module and it would automatically allow you to do that in the query config ( or quick if you want to use an ORM framework ). CF ORM’s hibernate criteria queries can do the same thing.

Is it not finding the method on the query argument or is it not compiling. If it’s not finding the method, then I would check the Javadocs ( or dump the metadata ) for that object to make sure your typing is correct. Once you are in Java land, method signatures are very specific and strongly typed.

@jclausen
I’m working with an old legacy application with vanilla cfml querying. I don’t get options on the framework level, so I’m trying to build a few small utilities to make my life simpler.

It’s not finding the method, as if there’s a type mismatch. But again, when I print the name of the class, it matches directly.

The query object, itself, is going to have native CFML type facades in it, so you would have to use those classes in your Java file.

Personally, I would suggest just grabbing this method from qb, throwing it in a cfscript block, and using it as a UDF in your application: qb/QueryUtils.cfc at main · coldbox-modules/qb · GitHub

There are many ways to convert a query to an array of structs natively in CFML. Trying to compile custom Java code sounds like a ton more work and may stop working in future CF releases if the internal classes change!

So long as you’re on CF 2021 or later, you can simply add returntype=”array” on your cfquery/queryexecute and you’re done! This will give you back what you want with no additional effort.

And if you’re on an older version of ACF, then you can still do this with only a few lines of CFML code

var arrayOfStructs = myQry.reduce((result, row) => {
  result.append(row);
  return result;
}, []);

Here’s a working sample on tryCF.com

Thanks for the replies! We’re finally about to update from 2018 so there are a few options here for me. Thanks!