{"record":{"id":"6a6ba8f01fe221f3","repo":"baomidou/mybatis-plus","slug":"cannot-find-class","errorCode":null,"errorMessage":"Cannot find class: {}","messagePattern":"Cannot find class: (.+?)","errorType":"exception","errorClass":"ClassNotFoundException","httpStatus":null,"severity":"error","filePath":"mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/ClassUtils.java","lineNumber":185,"sourceCode":"    public static Class<?> toClassConfident(String name, ClassLoader classLoader) {\n        try {\n            return loadClass(name, getClassLoaders(classLoader));\n        } catch (ClassNotFoundException e) {\n            throw ExceptionUtils.mpe(\"找不到指定的class！请仅在明确确定会有 class 的时候，调用该方法\", e);\n        }\n    }\n\n    private static Class<?> loadClass(String className, ClassLoader[] classLoaders) throws ClassNotFoundException {\n        for (ClassLoader classLoader : classLoaders) {\n            if (classLoader != null) {\n                try {\n                    return Class.forName(className, true, classLoader);\n                } catch (ClassNotFoundException e) {\n                    // ignore\n                }\n            }\n        }\n        throw new ClassNotFoundException(\"Cannot find class: \" + className);\n    }\n\n\n    /**\n     * Determine the name of the package of the given class,\n     * e.g. \"java.lang\" for the {@code java.lang.String} class.\n     *\n     * @param clazz the class\n     * @return the package name, or the empty String if the class\n     * is defined in the default package\n     */\n    public static String getPackageName(Class<?> clazz) {\n        Assert.notNull(clazz, \"Class must not be null\");\n        return getPackageName(clazz.getName());\n    }\n\n    /**\n     * Determine the name of the package of the given fully-qualified class name,","sourceCodeStart":167,"sourceCodeEnd":203,"githubUrl":"https://github.com/baomidou/mybatis-plus/blob/bf67d907478c724120bf76292da54abf9e73c2b3/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/ClassUtils.java#L167-L203","documentation":"ClassUtils.loadClass iterates every supplied ClassLoader and calls Class.forName(className, true, classLoader). If none of them can resolve the class, a ClassNotFoundException with the message 'Cannot find class: <name>' is thrown. This is the terminal failure of mybatis-plus's class resolution utility, used when the framework reflectively loads classes named in configuration (e.g. type handlers, entity classes, interceptors).","triggerScenarios":"Calling ClassUtils.toClassConfident(name) / loadClass with a fully-qualified class name that is not on any of the candidate classloaders (context classloader, ClassUtils's own classloader). Typical entry points: specifying a nonexistent className in mybatis-plus configuration, entity scanning resolving a class name built from a table name, or running in an environment (OSGi, war on some servers, fat-jar relocations) where the context classloader cannot see application classes.","commonSituations":"Typos in configured class names; shaded/relocated jars where the package prefix changed; complex containers (Tomcat war deployment, Spring Boot executable jar with custom launcher) where Thread.currentThread().getContextClassLoader() differs from the classloader that loaded mybatis-plus; missing dependency on the classpath.","solutions":["Verify the fully-qualified class name string is spelled correctly and matches the actual package (watch out for shading/relocation prefixes).","Confirm the jar containing the class is on the runtime classpath of the module that performs the reflective load.","In container/classloader-mismatch situations, set Thread.currentThread().setContextClassLoader(targetClassLoader) before the mybatis-plus call so the context loader can resolve the class.","Reproduce with Class.forName(name) in the same deployment to prove it is a classpath/classloader problem and not a mybatis-plus bug."],"exampleFix":"// before: class only visible to app classloader, context loader misses it\nString name = \"com.acme.biz.CustomTypeHandler\";\nClass<?> clazz = ClassUtils.toClassConfident(name); // ClassNotFoundException\n\n// after: align the context classloader before resolution\nThread.currentThread().setContextClassLoader(getClass().getClassLoader());\nClass<?> clazz = ClassUtils.toClassConfident(name);","handlingStrategy":"validation","validationCode":"String name = \"com.acme.biz.Thing\";\nboolean resolvable;\ntry {\n    Class.forName(name, true, Thread.currentThread().getContextClassLoader());\n    resolvable = true;\n} catch (ClassNotFoundException e) {\n    resolvable = false;\n}\nif (!resolvable) { /* fix classpath/classloader before proceeding */ }","typeGuard":null,"tryCatchPattern":"try {\n    Class<?> clazz = ClassUtils.toClassConfident(name);\n} catch (ClassNotFoundException e) {\n    throw new IllegalStateException(\"Class not on runtime classpath: \" + name\n        + \" — check dependencies and context classloader\", e);\n}","preventionTips":["Validate configured class names at startup (fail fast with a clear message) instead of lazily during a request.","In embedded/container setups, set the context classloader explicitly before mybatis-plus initializes.","Pin and verify dependencies so reflective targets cannot disappear during upgrades or shading."],"tags":["classpath","classloader","reflection","configuration"],"backgroundTag":null,"analyzedSha":"bf67d907478c724120bf76292da54abf9e73c2b3","analyzedAt":"2026-08-14T15:17:09.543Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}