Search code examples
javajbossdrools

`Unable to find class` error in Drools


I'm trying to define a very simple function in Drools as below:

import java.util.List;

function int sumLengths(List<String> strings) {
    int counter = 0;
    for (String s : strings)
        counter += s.length();
    return counter;
}

but it gives me error:

Exception in thread "main" java.lang.RuntimeException: [ function sumLengths (line:5):
Unable to resolve type List<String> while building function. java.lang.ClassNotFoundException: Unable to find class 'List<String>' ]

any idea?


Solution

  • Perhaps it is an issue with generics (This points me to that conclusion). Have you tried the following (or similar):

    function int sumLengths(List strings) {
        int counter = 0;
        for (Object s : strings)
            counter += ((String) s).length();
        return counter;
    }
    

    If it doesn't work you could use this instead:

    function int sumLengths(String[] strings) {
        int counter = 0;
        int length = (strings != null) ? strings.length : -1;
        for (int idx = 0; idx < length; ++idx) {
            counter += strings[idx].length();
        }
        return counter;
    }