karatelabs/karate · error · JsErrorException

is not a constructor

Error message

 is not a constructor

What it means

Karate's JS engine throws this when JS code tries to use Java interop (`new Java.type('...')(...)` style construction) but no matching constructor can be found on the resolved Java class. JavaUtils.construct calls findConstructor; if none matches the supplied arguments (or the class has no public constructor), it wraps the failure as a JS TypeError '<TypeName> is not a constructor'.

Solutions

  1. Check the Java class actually has a public constructor matching the number and types of the arguments you pass.
  2. For abstract classes/interfaces, use a factory method instead, e.g. java.util.Map.of(...) or Java.type('java.util.HashMap').
  3. Convert JS values explicitly (e.g. use java.lang.Integer.valueOf) when overload resolution fails.
  4. Verify nested class access uses the correct syntax (Java.type('outer.inner') rather than property access on the outer type).

Example fix

// before
var List = Java.type('java.util.List');
var l = new List();
// after
var ArrayList = Java.type('java.util.ArrayList');
var l = new ArrayList();
Defensive patterns

Strategy: try-catch

Validate before calling

var Clz = Java.type('java.util.HashMap');
// confirm a public constructor exists by attempting a no-arg build in setup, not at test time

Type guard

function isConstructible(clazz) { try { return typeof clazz === 'object' && clazz.getClass && !java.lang.reflect.Modifier.isAbstract(clazz.getClass().getModifiers()); } catch (e) { return false; } }

Try / catch

try { var obj = new Clz(); } catch (e) { if (String(e).indexOf('is not a constructor') !== -1) { obj = Clz.getDefault ? Clz.getDefault() : null; } else { throw e; } }

Prevention

When it happens

Trigger: Calling `new` on a Java class handle with argument types/counts that match no declared constructor, or on a class/inner class that is abstract, an interface, or has only non-public constructors.

Common situations: Typo in the fully-qualified class name resolving to the wrong type; passing JS strings/numbers that don't convert to the expected Java parameter types; trying to instantiate abstract classes like java.util.Map or nested classes addressed incorrectly.

Related errors


AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12). Data as JSON: /api/errors/0e86d80020825b2f. Report an issue: GitHub.

Appendix: source

Thrown at karate-js/src/main/java/io/karatelabs/js/JavaUtils.java:52

        if (List.class.isAssignableFrom(clazz)) return "Array";
        if (clazz.isArray()) return "Array";
        if (clazz == String.class) return "String";
        if (Number.class.isAssignableFrom(clazz) || clazz == int.class
                || clazz == double.class || clazz == long.class
                || clazz == float.class || clazz == short.class
                || clazz == byte.class) return "Number";
        if (clazz == Boolean.class || clazz == boolean.class) return "Boolean";
        if (clazz == Character.class || clazz == char.class) return "String";
        if (Set.class.isAssignableFrom(clazz)) return "Set";
        return clazz.getSimpleName();
    }

    static Object construct(Class<?> clazz, Object[] args) {
        try {
            Constructor<?> constructor = findConstructor(clazz, args);
            return constructor.newInstance(args);
        } catch (Exception e) {
            throw JsErrorException.typeError(jsTypeName(clazz) + " is not a constructor");
        }
    }

    static Object invokeStatic(Class<?> clazz, String name, Object[] args) {
        Method method = findMethod(clazz, name, args);
        if (method == null) {
            throw JsErrorException.typeError("." + name + " is not a function (called on " + jsTypeName(clazz) + ")");
        }
        try {
            return invoke(null, method, args);
        } catch (InvocationTargetException e) {
            Throwable cause = e.getCause();
            if (cause instanceof RuntimeException re) {
                throw re;
            }
            if (cause instanceof Error err) {
                throw err;
            }

View on GitHub (pinned to a22eb90246)