{"record":{"id":"53d419a59e7e1ad6","repo":"karatelabs/karate","slug":"java-type-class-not-found-classname","errorCode":null,"errorMessage":"Java.type: class not found: {className}","messagePattern":"Java\\.type: class not found: (.+?)","errorType":"exception","errorClass":"JsErrorException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsJava.java","lineNumber":50,"sourceCode":"            throw new RuntimeException(\"java bridge not enabled\");\n        }\n        this.bridge = bridge;\n    }\n\n    @Override\n    public Object jsGet(String name) {\n        return switch (name) {\n            case \"type\" -> (JsInvokable) args -> {\n                String className = (String) args[0];\n                // forType() returns null on ClassNotFoundException — that\n                // null-as-sentinel contract is needed by PropertyAccess for\n                // the dotted-FQN probe, so we keep it. But here the script\n                // explicitly asked for this class; a null result must surface\n                // as a real error rather than silently propagating and\n                // failing later as \"cannot read properties of null\".\n                ExternalAccess type = bridge.forType(className);\n                if (type == null) {\n                    throw JsErrorException.typeError(\"Java.type: class not found: \" + className);\n                }\n                return type;\n            };\n            case \"to\" -> (JsInvokable) args -> {\n                if (args[0] instanceof ExternalAccess ja) {\n                    return ja.getJavaValue();\n                }\n                // TODO regex, functions, lambdas\n                return null;\n            };\n            default -> throw JsErrorException.typeError(\"no such api on Java: \" + name);\n        };\n    }\n\n}\n","sourceCodeStart":32,"sourceCodeEnd":66,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsJava.java#L32-L66","documentation":"Java.type('X') could not resolve the class name through the type bridge: bridge.forType(className) returned null. Karate surfaces this immediately as a TypeError so the failure is not silently propagated and later misreported as 'cannot read properties of null'.","triggerScenarios":"Java.type('com.example.Missing') where the class does not exist, the FQN is misspelled, the class is in a jar not on the classpath, or an inner class is referenced with a dot instead of '$'.","commonSituations":"Typos in fully-qualified names; running a feature that needs a dependency not declared in the pom/build; class present in a different classloader (e.g. IDE vs CLI); migrating code between projects with different dependencies.","solutions":["Verify the exact fully-qualified class name, using '$' for nested classes (com.example.Outer$Inner).","Add the jar/dependency containing the class to the classpath (pom.xml, build.gradle, or -cp).","Confirm with `karate.log(Java.type('com.example.Foo'))` on a class known to exist to validate classpath setup.","Check that the class is visible to the same classloader Karate runs under (IDE module vs Maven test classpath)."],"exampleFix":"// before\nvar Cls = Java.type('com.example.util.Paylod'); // typo\n// after\nvar Cls = Java.type('com.example.util.Payload');","handlingStrategy":"try-catch","validationCode":"// confirm the dependency is on the classpath before running the feature (e.g. mvn dependency:tree | grep my-lib)","typeGuard":null,"tryCatchPattern":"var Cls;\ntry { Cls = Java.type('com.example.Foo'); } catch (e) { if (String(e).indexOf('class not found') !== -1) { karate.abort('missing dependency for Foo'); } else { throw e; } }","preventionTips":["Copy FQNs from the source/jar rather than typing them","Use $ syntax for nested classes","Declare test-scope dependencies in the build so CLI and IDE classpaths match","Smoke-test Java.type for critical classes in an init step"],"tags":["javascript","java-interop","classpath","class-not-found"],"backgroundTag":"class-not-found","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}