json-path/JsonPath · error · InvalidPathException

Function of name: cannot be created

Error message

Function of name:  cannot be created

What it means

After a function name IS found in the FUNCTIONS map, newFunction() instantiates it via Class.newInstance(). If instantiation fails (no public no-arg constructor, constructor throws, access/initialization errors), it wraps the cause in InvalidPathException('Function of name: X cannot be created').

Solutions

  1. Ensure the PathFunction implementation is public with a public no-argument constructor.
  2. Check the wrapped cause (getCause()) to find the real instantiation error.
  3. Remove any constructor logic that can throw; defer setup to the invoke() method.
  4. If using a custom classloader/OSGi, verify the class is loadable and instantiable at runtime.

Example fix

// before
class MyFunc implements PathFunction {
    public MyFunc(String cfg) { ... } // no no-arg ctor
}
// after
public class MyFunc implements PathFunction {
    public MyFunc() { }
    public Object invoke(EvaluationContext ctx, List<Parameter> params) { ... }
}
Defensive patterns

Strategy: validation

Validate before calling

Class<?> c = PathFunctionFactory.FUNCTIONS.get(name);
if (c == null) throw new InvalidPathException("not registered");
if (!java.lang.reflect.Modifier.isPublic(c.getModifiers())) throw new IllegalStateException("function class must be public");
try { c.getDeclaredConstructor().newInstance(); } catch (Exception e) { throw new IllegalStateException("not instantiable", e); }

Try / catch

try {
    JsonPath.read(json, path);
} catch (InvalidPathException e) {
    if (e.getCause() != null) log.warn("Function instantiation failed", e.getCause());
    throw e;
}

Prevention

When it happens

Trigger: A registered custom PathFunction class lacks a public no-argument constructor, throws in its constructor/static initializer, or is not public — encountered whenever a path invokes that function, e.g. $.data.myCustomFunc().

Common situations: Custom functions defined with constructor parameters, private inner classes, functions throwing in constructors when environment/config is missing, or hot-reload/classloader issues in containers where the class can no longer be instantiated.

Related errors


AI-assisted analysis of json-path/JsonPath@62a4c9f0f6 (2026-09-11). Data as JSON: /api/errors/50231dac2f1ebde8. Report an issue: GitHub.

Appendix: source

Thrown at json-path/src/main/java/com/jayway/jsonpath/internal/function/PathFunctionFactory.java:84

     * @see PathFunction
     *
     * @param name
     *      The name of the function
     *
     * @return
     *      The implementation of a function
     *
     * @throws InvalidPathException
     */
    public static PathFunction newFunction(String name) throws InvalidPathException {
        Class functionClazz = FUNCTIONS.get(name);
        if(functionClazz == null){
            throw new InvalidPathException("Function with name: " + name + " does not exist.");
        } else {
            try {
                return (PathFunction)functionClazz.newInstance();
            } catch (Exception e) {
                throw new InvalidPathException("Function of name: " + name + " cannot be created", e);
            }
        }
    }
}

View on GitHub (pinned to 62a4c9f0f6)