{"id":33,"date":"2026-03-14T22:22:57","date_gmt":"2026-03-14T14:22:57","guid":{"rendered":"http:\/\/47.118.30.30\/?p=33"},"modified":"2026-03-15T11:40:56","modified_gmt":"2026-03-15T03:40:56","slug":"classloader","status":"publish","type":"post","link":"https:\/\/arknight.wiki\/index.php\/2026\/03\/14\/classloader\/","title":{"rendered":"classloader"},"content":{"rendered":"<h1><strong>classloader<\/strong>\uff1a<\/h1>\n<p><strong>JVM\u662f\u4ec0\u4e48<\/strong>\uff1a\u7c7b\u4f3c\u4e8e\u7ffb\u8bd1\u5668\uff0c\u5c06\u540c\u4e00\u4e2ajava\u4ee3\u7801\u7ffb\u8bd1\u7ed9\u4e0d\u540c\u7684\u64cd\u4f5c\u7cfb\u7edf\uff0c\u4ee5\u8fbe\u5230\u901a\u7528\u7684\u6548\u679c\uff0c\u5982windows\u9002\u914d\u7684JVM\u53ef\u4ee5\u5c06java\u7ffb\u8bd1\u4f7fwindows\u53ef\u4ee5\u8fd0\u884c\u3002<\/p>\n<p><strong>classloader<\/strong> \u662f\u4ec0\u4e48\uff1a\u5c06<code>.class<\/code>\u52a0\u8f7d\u5230JVM\u4e2d\uff0c\u4e3a\u8282\u7ea6\u8d44\u6e90\uff0c\u63d0\u9ad8\u901f\u5ea6\u548c\u6548\u7387\uff0cJVM\u4e0d\u4f1a\u4e00\u6b21\u6027\u5c06\u6240\u6709\u7c7b\u6587\u4ef6\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\uff0c\u800c\u662f\u6839\u636e\u9700\u8981\u52a8\u6001\u52a0\u8f7d\u3002<\/p>\n<p><strong>\u7f16\u8bd1\u8fc7\u7a0b<\/strong>\uff1a\u6211\u4eec\u5199\u7684<code>.java<\/code>\u6e90\u7801\u4e0d\u80fd\u76f4\u63a5\u8fd0\u884c\uff0c\u800c\u662f\u4f1a\u7ecf\u8fc7\u7f16\u8bd1\u5728\u540c\u76ee\u5f55\u4e0b\u751f\u6210<code>.class<\/code>\u5b57\u8282\u7801\u6587\u4ef6\u518d\u4ee5\u8fd9\u79cd\u53ef\u88abJVM\u8bc6\u522b\u7684\u5f62\u5f0f\u62ff\u7ed9JVM\u7ffb\u8bd1\u7ed9\u64cd\u4f5c\u7cfb\u7edf\u8fd0\u884c\u3002<\/p>\n<h3>\u6df1\u5165\u7c7b\u52a0\u8f7d\u8fc7\u7a0b\uff1a<\/h3>\n<h5>windows\u4e0b\u7684\u73af\u5883\u53d8\u91cf\u914d\u7f6e\uff1a<\/h5>\n<p>JAVA_HOME\uff1a\u544a\u77e5\u7cfb\u7edfJDK\u7684\u5b89\u88c5\u4f4d\u7f6e<\/p>\n<p>PATH\uff1a\u8fd9\u4e2a\u53d8\u91cf\u7684\u4f5c\u7528\u662f\u8ba9\u4f60\u5728\u547d\u4ee4\u884c\u4e2d\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u67d0\u4e9b\u547d\u4ee4\uff0c\u800c\u4e0d\u9700\u8981\u8f93\u5165\u5b83\u4eec\u7684\u5b8c\u6574\u8def\u5f84\u3002\u6bd4\u5982\uff0c\u8fd0\u884c\u00a0<code>javac<\/code>\u00a0\u548c\u00a0<code>java<\/code>\u00a0\u547d\u4ee4\u65f6\uff0c\u5982\u679c\u6ca1\u6709\u5c06 JDK \u4e2d\u7684\u00a0<code>bin<\/code>\u00a0\u76ee\u5f55\u6dfb\u52a0\u5230 PATH \u4e2d\uff0c\u5c31\u9700\u8981\u8f93\u5165\u5168\u8def\u5f84\uff0c\u975e\u5e38\u9ebb\u70e6\u3002<\/p>\n<p>CLASSPAYH\uff1a\u8fd9\u4e2a\u53d8\u91cf\u662f\u7528\u6765\u6307\u5411\u00a0<code>.jar<\/code>\u00a0\u5305\u6216\u00a0<code>.class<\/code>\u00a0\u6587\u4ef6\u6240\u5728\u8def\u5f84\u7684\u3002JVM \u5728\u8fd0\u884c\u65f6\u4f1a\u6839\u636e CLASSPATH \u627e\u5230\u4f60\u9700\u8981\u52a0\u8f7d\u7684\u7c7b\u6587\u4ef6\u6216\u4f9d\u8d56\u5305\u3002<\/p>\n<p>\u6ce8\uff1aCLASSPATH \u7684\u503c\u901a\u5e38\u4ee5\u00a0<code>.;<\/code>\u00a0\u5f00\u5934\uff0c\u5176\u4e2d\u00a0<code>.<\/code>\u00a0\u8868\u793a\u5f53\u524d\u76ee\u5f55\u3002\u5982\u679c\u7701\u7565\u4e86\u8fd9\u90e8\u5206\uff0cJVM \u53ef\u80fd\u65e0\u6cd5\u8bc6\u522b\u5f53\u524d\u76ee\u5f55\u4e0b\u7684\u7c7b\u6587\u4ef6\u3002<\/p>\n<h5>\u4e09\u4e2a\u7cfb\u7edf\u7c7b\u52a0\u8f7d\u5668\uff1a<\/h5>\n<p>Bootstrap ClassLoader\uff08\u5f15\u5bfc\u7c7b\u52a0\u8f7d\u5668\uff09\uff1a\u8fd9\u662fJava\u4e2d\u6700\u9876\u5c42\u7684\u7c7b\u52a0\u8f7d\u5668\uff0c\u8d1f\u8d23\u52a0\u8f7d\u6838\u5fc3\u7c7b\u5e93\u3002\u5b83\u4f1a\u52a0\u8f7d\u00a0<code>%JRE_HOME%lib<\/code>\u00a0\u76ee\u5f55\u4e0b\u7684\u91cd\u8981\u6587\u4ef6\uff0c\u4f8b\u5982\u00a0<code>rt.jar<\/code>\u3001<code>resources.jar<\/code>\u3001<code>charsets.jar<\/code>\u00a0\u7b49\u57fa\u7840\u7c7b\u5e93\u3002<\/p>\n<p>\u6ce8\uff1a\u5f15\u5bfc\u7c7b\u52a0\u8f7d\u5668\u7684\u8def\u5f84\u662f\u53ef\u4ee5\u6539\u7684\uff0c\u53ef\u4ee5\u6dfb\u52a0\u65b0\u7684\u9ed8\u8ba4\u52a0\u8f7d\u8def\u5f84<\/p>\n<pre><code>java -Xbootclasspath\/a:path\n\/\/ \u8fd9\u6837\u4f1a\u5c06\u6307\u5b9a\u7684\u8def\u5f84\u6587\u4ef6\u8ffd\u52a0\u5230\u9ed8\u8ba4\u7684\u5f15\u5bfc\u52a0\u8f7d\u8def\u5f84\u4e2d\u3002<\/code><\/pre>\n<p>Extension ClassLoader\uff08\u6269\u5c55\u7c7b\u52a0\u8f7d\u5668\uff09\uff1a\u6269\u5c55\u7c7b\u52a0\u8f7d\u5668\u7528\u4e8e\u52a0\u8f7d\u00a0<code>%JRE_HOME%libext<\/code>\u00a0\u76ee\u5f55\u4e0b\u7684\u00a0<code>.jar<\/code>\u00a0\u5305\u548c\u00a0<code>.class<\/code>\u00a0\u6587\u4ef6\u3002\u9664\u6b64\u4e4b\u5916\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7 JVM \u53c2\u6570\u00a0<code>-Djava.ext.dirs=\u8def\u5f84<\/code>\u00a0\u6307\u5b9a\u5176\u4ed6\u6269\u5c55\u52a0\u8f7d\u76ee\u5f55\u3002<\/p>\n<p>Application ClassLoader\uff08\u5e94\u7528\u7c7b\u52a0\u8f7d\u5668\uff09\uff1a\u4e5f\u88ab\u79f0\u4e3a\u00a0<code>System ClassLoader<\/code>\uff0c\u5b83\u662f\u7528\u6237\u6700\u5e38\u63a5\u89e6\u7684\u7c7b\u52a0\u8f7d\u5668\uff0c\u4e3b\u8981\u8d1f\u8d23\u52a0\u8f7d\u5f53\u524d\u5e94\u7528\u7a0b\u5e8f\u7684\u7c7b\u8def\u5f84\uff08<code>classpath<\/code>\uff09\u4e2d\u7684\u6240\u6709\u7c7b\u3002\u7528\u6237\u81ea\u5df1\u7f16\u5199\u7684\u7c7b\u548c\u4f9d\u8d56\u5e93\u5927\u591a\u7531\u5b83\u52a0\u8f7d\u3002<\/p>\n<h5>\u7c7b\u52a0\u8f7d\u52a0\u8f7d\u987a\u5e8f\uff1a<\/h5>\n<p><strong>Bootstrap CLassloder-&gt;Extention ClassLoader-&gt;AppClassLoader<\/strong><\/p>\n<p>\u5148\u770b<code>sun.misc.Launcher<\/code>,\u5b83\u662f\u4e00\u4e2ajava\u865a\u62df\u673a\u7684\u5165\u53e3\u5e94\u7528\u3002<\/p>\n<pre><code>public class Launcher {\n    private static Launcher launcher = new Launcher();\n    private static String bootClassPath =\n        System.getProperty(\"sun.boot.class.path\");\n\n    public static Launcher getLauncher() {\n        return launcher;\n    }\n\n    private ClassLoader loader;\n\n    public Launcher() {\n        \/\/ Create the extension class loader\n        ClassLoader extcl;\n        try {\n            extcl = ExtClassLoader.getExtClassLoader();\n        } catch (IOException e) {\n            throw new InternalError(\n                \"Could not create extension class loader\", e);\n        }\n\n        \/\/ Now create the class loader to use to launch the application\n        try {\n            loader = AppClassLoader.getAppClassLoader(extcl);\n        } catch (IOException e) {\n            throw new InternalError(\n                \"Could not create application class loader\", e);\n        }\n\n        \/\/\u8bbe\u7f6eAppClassLoader\u4e3a\u7ebf\u7a0b\u4e0a\u4e0b\u6587\u7c7b\u52a0\u8f7d\u5668\uff0c\u8fd9\u4e2a\u6587\u7ae0\u540e\u9762\u90e8\u5206\u8bb2\u89e3\n        Thread.currentThread().setContextClassLoader(loader);\n    }\n\n    \/*\n     * Returns the class loader used to launch the main application.\n     *\/\n    public ClassLoader getClassLoader() {\n        return loader;\n    }\n    \/*\n     * The class loader used for loading installed extensions.\n     *\/\n    static class ExtClassLoader extends URLClassLoader {}\n\n\/**\n     * The class loader used for loading from java.class.path.\n     * runs in a restricted security context.\n     *\/\n    static class AppClassLoader extends URLClassLoader {}\n}<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u51fa\uff1a<\/p>\n<ul>\n<li>Launcher\u521d\u59cb\u5316\u4e86ExtClassLoader\u548cAppClassLoader\u3002<\/li>\n<li>Launcher\u4e2d\u5e76\u6ca1\u6709\u770b\u89c1BootstrapClassLoader\uff0c\u4f46\u901a\u8fc7<code>System.getProperty(\"sun.boot.class.path\")<\/code>\u5f97\u5230\u4e86\u5b57\u7b26\u4e32<code>bootClassPath<\/code>,\u8fd9\u4e2a\u5e94\u8be5\u5c31\u662fBootstrapClassLoader\u52a0\u8f7d\u7684jar\u5305\u8def\u5f84\u3002<\/li>\n<li>extcl\u662f\u4e00\u4e2aExtClassLoader\u5b9e\u4f8b<\/li>\n<\/ul>\n<p>ExtClassLoader\u5de5\u4f5c\u6d41\u7a0b\uff1a<\/p>\n<pre><code>\/*\n     * The class loader used for loading installed extensions.\n     *\/\n    static class ExtClassLoader extends URLClassLoader {\n\n        static {\n            ClassLoader.registerAsParallelCapable();\n        }\n\n        \/**\n         * create an ExtClassLoader. The ExtClassLoader is created\n         * within a context that limits which files it can read\n         *\/\n        public static ExtClassLoader getExtClassLoader() throws IOException\n        {\n            final File[] dirs = getExtDirs();\n\n            try {\n                \/\/ Prior implementations of this doPrivileged() block supplied\n                \/\/ aa synthesized ACC via a call to the private method\n                \/\/ ExtClassLoader.getContext().\n\n                return AccessController.doPrivileged(\n                    new PrivilegedExceptionAction&lt;ExtClassLoader&gt;() {\n                        public ExtClassLoader run() throws IOException {\n                            int len = dirs.length;\n                            for (int i = 0; i &lt; len; i++) {\n                                MetaIndex.registerDirectory(dirs[i]);\n                            }\n                            return new ExtClassLoader(dirs);\n                        }\n                    });\n            } catch (java.security.PrivilegedActionException e) {\n                throw (IOException) e.getException();\n            }\n        }\n\n        private static File[] getExtDirs() {\n            String s = System.getProperty(\"java.ext.dirs\");\n            File[] dirs;\n            if (s != null) {\n                StringTokenizer st =\n                    new StringTokenizer(s, File.pathSeparator);\n                int count = st.countTokens();\n                dirs = new File[count];\n                for (int i = 0; i &lt; count; i++) {\n                    dirs[i] = new File(st.nextToken());\n                }\n            } else {\n                dirs = new File[0];\n            }\n            return dirs;\n        }\n\n......\n    }<\/code><\/pre>\n<p>\u7b80\u8981\u89e3\u91ca\uff1a\u521b\u5efa\u4e00\u4e2a\u4ece<code>URLClassLoader<\/code>extends\u7684\u7c7b<code>Extention ClassLoader<\/code>,\u5b83\u7684<code>getExtDirs()<\/code>\u65b9\u6cd5\u5c06<code>java.ext.dirs<\/code>\u76ee\u5f55\u5b57\u7b26\u4e32\u8f6c\u5316\u4e3a\u4e00\u4e2a\u4e2a\u6587\u4ef6\u5bf9\u8c61\uff0c\u7136\u540e\u96c6\u5408\u4e3a\u4e00\u4e2a\u6587\u4ef6\u6570\u7ec4\uff0c\u518d\u4ea4\u7ed9<code>getExtClassLoader()<\/code>\u65b9\u6cd5\u5904\u7406\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u5c06\u6587\u4ef6\u6570\u7ec4\u8f6c\u5316\u4e3aURL\u6570\u7ec4\uff0c\u4ea4\u7ed9ExtClassLoader\u6784\u9020\u65b9\u6cd5\u5904\u7406\uff0c\u8fd9\u6837\u4f1a\u8c03\u7528\u5b83\u7684\u7236\u7c7b\uff0c\u5373<code>URLClassLoader<\/code>\u7684\u6784\u9020\u65b9\u6cd5\u5b8c\u6210\u521d\u59cb\u5316\u3002\u53ef\u4ee5\u770b\u5230\uff0c<code>java.ext.dirs<\/code>\u5c31\u662f\u5b83\u7684\u52a0\u8f7d\u8def\u5f84\u3002<\/p>\n<p>AppClassLoader\u5de5\u4f5c\u6d41\u7a0b:<\/p>\n<pre><code>\/**\n * The class loader used for loading from java.class.path.\n * runs in a restricted security context.\n *\/\nstatic class AppClassLoader extends URLClassLoader {\n\n    public static ClassLoader getAppClassLoader(final ClassLoader extcl)\n        throws IOException\n    {\n        final String s = System.getProperty(\"java.class.path\");\n        final File[] path = (s == null) ? new File[0] : getClassPath(s);\n\n        return AccessController.doPrivileged(\n            new PrivilegedAction&lt;AppClassLoader&gt;() {\n                public AppClassLoader run() {\n                URL[] urls =\n                    (s == null) ? new URL[0] : pathToURLs(path);\n                return new AppClassLoader(urls, extcl);\n            }\n        });\n    }\n    ......\n}<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u51fa<code>AppClassLoader<\/code>\u52a0\u8f7d\u7684\u5c31\u662f<code>java.class.path<\/code>\u4e0b\u7684\u8def\u5f84\u3002<\/p>\n<h5>\u6bcf\u4e2a\u7c7b\u52a0\u8f7d\u5668\u90fd\u6709\u4e00\u4e2a\u7236\u52a0\u8f7d\u5668:<\/h5>\n<p>\u6bcf\u4e2a\u7c7b\u52a0\u8f7d\u5668\u90fd\u6709\u4e00\u4e2a\u7236\u7c7b\u52a0\u8f7d\u5668\uff0c<code>AppClassLoader<\/code>\u7684\u7236\u7c7b\u52a0\u8f7d\u5668\u662f<code>ExtClassLoader<\/code>\uff0c\u800c<code>ExtClassLoader<\/code>\u7684\u5219\u662f<code>Bootstrap ClassLoader<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7<code>getParent()<\/code>\u65b9\u6cd5\u5f97\u5230\u7236\u7c7b\u52a0\u8f7d\u5668\u3002\u4e0d\u8fc7\u8981\u6ce8\u610f\u7684\u662f\uff0c\u7236\u52a0\u8f7d\u5668\u4e0d\u662f\u7236\u7c7b\u3002<\/p>\n<p>\u5148\u770b<code>getParent()<\/code>\u65b9\u6cd5\u7684\u5b9e\u73b0\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u5728<code>ClassLoader.java<\/code>\u4e2d\uff1a<\/p>\n<pre><code>public abstract class ClassLoader {\n\n    \/\/ The parent class loader for delegation\n    \/\/ Note: VM hardcoded the offset of this field, thus all new fields\n    \/\/ must be added *after* it.\n    private final ClassLoader parent;\n    \/\/ The class loader for the system\n        \/\/ @GuardedBy(\"ClassLoader.class\")\n    private static ClassLoader scl;\n\n    private ClassLoader(Void unused, ClassLoader parent) {\n        this.parent = parent;\n        ...\n    }\n    protected ClassLoader(ClassLoader parent) {\n        this(checkCreateClassLoader(), parent);\n    }\n    protected ClassLoader() {\n        this(checkCreateClassLoader(), getSystemClassLoader());\n    }\n    public final ClassLoader getParent() {\n        if (parent == null)\n            return null;\n        return parent;\n    }\n    public static ClassLoader getSystemClassLoader() {\n        initSystemClassLoader();\n        if (scl == null) {\n            return null;\n        }\n        return scl;\n    }\n\n    private static synchronized void initSystemClassLoader() {\n        if (!sclSet) {\n            if (scl != null)\n                throw new IllegalStateException(\"recursive invocation\");\n            sun.misc.Launcher l = sun.misc.Launcher.getLauncher();\n            if (l != null) {\n                Throwable oops = null;\n                \/\/\u901a\u8fc7Launcher\u83b7\u53d6ClassLoader\n                scl = l.getClassLoader();\n                try {\n                    scl = AccessController.doPrivileged(\n                        new SystemClassLoaderAction(scl));\n                } catch (PrivilegedActionException pae) {\n                    oops = pae.getCause();\n                    if (oops instanceof InvocationTargetException) {\n                        oops = oops.getCause();\n                    }\n                }\n                if (oops != null) {\n                    if (oops instanceof Error) {\n                        throw (Error) oops;\n                    } else {\n                        \/\/ wrap the exception\n                        throw new Error(oops);\n                    }\n                }\n            }\n            sclSet = true;\n        }\n    }\n}<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230<code>getParent()<\/code>\u5b9e\u9645\u4e0a\u8fd4\u56de\u7684\u5c31\u662f\u4e00\u4e2aClassLoader\u5bf9\u8c61parent\uff0cparent\u7684\u8d4b\u503c\u662f\u5728ClassLoader\u5bf9\u8c61\u7684\u6784\u9020\u65b9\u6cd5\u4e2d\uff0c\u5b83\u6709\u4e24\u4e2a\u60c5\u51b5\uff1a 1. \u7531\u5916\u90e8\u7c7b\u521b\u5efaClassLoader\u65f6\u76f4\u63a5\u6307\u5b9a\u4e00\u4e2aClassLoader\u4e3aparent\u3002 2. \u7531<code>getSystemClassLoader()<\/code>\u65b9\u6cd5\u751f\u6210\uff0c\u4e5f\u5c31\u662f\u5728sun.misc.Laucher\u901a\u8fc7<code>getClassLoader()<\/code>\u83b7\u53d6\uff0c\u4e5f\u5c31\u662fAppClassLoader\u3002\u76f4\u767d\u7684\u8bf4\uff0c\u4e00\u4e2aClassLoader\u521b\u5efa\u65f6\u5982\u679c\u6ca1\u6709\u6307\u5b9aparent\uff0c\u90a3\u4e48\u5b83\u7684parent\u9ed8\u8ba4\u5c31\u662fAppClassLoader\u3002<\/p>\n<p>\u7136\u540e\u770b\u4ee3\u7801\uff1a<\/p>\n<pre><code>\/\/ \u91cd\u70b9\u662f\u8fd9\u4e00\u6bb5\u4ee3\u7801\nClassLoader extcl;\nextcl = ExtClassLoader.getExtClassLoader();\nloader = AppClassLoader.getAppClassLoader(extcl);<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u51faextcl\u662f\u4e00\u4e2a<code>ExtClassLoader<\/code>\u5b9e\u4f8b\uff0c\u7136\u540e\u4f1a\u4f20\u7ed9<code>getAppClassLoader()<\/code>,\u53ef\u77e5AppClassLoader\u7684\u7236\u52a0\u8f7d\u5668\u5c31\u662fExtClassLoader<\/p>\n<h5>Bootstrap ClassLoader\uff1a<\/h5>\n<p>\u4e0d\u540c\u4e8e\u5176\u4ed6\u7528java\u5b9e\u73b0\u7684\u52a0\u8f7d\u5668\uff0c\u5b83\u662f\u4e00\u4e2a\u7528C++\u5b9e\u73b0\u7684\u7279\u6b8a\u52a0\u8f7d\u5668\uff0c\u5c5e\u4e8ejava\u865a\u62df\u673a\u7684\u4e00\u90e8\u5206\u3002\u5b83\u6ca1\u6709\u7236\u52a0\u8f7d\u5668\u4f46\u662f\u53ef\u4ee5\u6210\u4e3a\u5176\u4ed6\u7684\u52a0\u8f7d\u5668\u7684\u7236\u52a0\u8f7d\u5668\u3002\u6bd4\u5982<code>ExtClassLoader<\/code>\u3002\u8fd9\u4e5f\u53ef\u4ee5\u89e3\u91ca\u901a\u8fc7ExtClassLoader\u7684getParent\u65b9\u6cd5\u83b7\u53d6\u4e3aNull\u7684\u73b0\u8c61\u3002<\/p>\n<h5>\u53cc\u4eb2\u59d4\u6258\uff1a<\/h5>\n<p>\u00a0\u4e00\u4e2a\u7c7b\u52a0\u8f7d\u5668\u67e5\u627eclass\u548cresource\u65f6\uff0c\u662f\u901a\u8fc7\u201c\u59d4\u6258\u6a21\u5f0f\u201d\u8fdb\u884c\u7684\uff0c\u5b83\u9996\u5148\u5224\u65ad\u8fd9\u4e2aclass\u662f\u4e0d\u662f\u5df2\u7ecf\u52a0\u8f7d\u6210\u529f\uff0c\u5982\u679c\u6ca1\u6709\u7684\u8bdd\u5b83\u5e76\u4e0d\u662f\u81ea\u5df1\u8fdb\u884c\u67e5\u627e\uff0c\u800c\u662f\u5148\u901a\u8fc7\u7236\u52a0\u8f7d\u5668\uff0c\u7136\u540e\u9012\u5f52\u4e0b\u53bb\uff0c\u76f4\u5230Bootstrap ClassLoader\uff0c\u5982\u679cBootstrap classloader\u627e\u5230\u4e86\uff0c\u76f4\u63a5\u8fd4\u56de\uff0c\u5982\u679c\u6ca1\u6709\u627e\u5230\uff0c\u5219\u4e00\u7ea7\u4e00\u7ea7\u8fd4\u56de\uff0c\u6700\u540e\u5230\u8fbe\u81ea\u8eab\u53bb\u67e5\u627e\u8fd9\u4e9b\u5bf9\u8c61\u3002\u8fd9\u79cd\u673a\u5236\u5c31\u53eb\u505a\u53cc\u4eb2\u59d4\u6258\u3002<\/p>\n<p>\u8fd9\u4e2a\u673a\u5236\u5206\u4e3a\u4e24\u90e8\u5206\uff0c\u7b2c\u4e00\u90e8\u5206\u662f\u5411\u4e0a\u59d4\u6258\u7684\u8fc7\u7a0b\uff1a<\/p>\n<ul>\n<li>\u4e00\u4e2aAppClassLoader\u67e5\u627e\u8d44\u6e90\u65f6\uff0c\u5148\u770b\u770b\u7f13\u5b58\u662f\u5426\u6709\uff0c\u7f13\u5b58\u6709\u4ece\u7f13\u5b58\u4e2d\u83b7\u53d6\uff0c\u5426\u5219\u59d4\u6258\u7ed9\u7236\u52a0\u8f7d\u5668\u3002<\/li>\n<li>\u9012\u5f52\uff0c\u91cd\u590d\u7b2c1\u6b65\u9aa4\u7684\u64cd\u4f5c\u3002<\/li>\n<li>\u5982\u679cExtClassLoader\u4e5f\u6ca1\u6709\u52a0\u8f7d\u8fc7\uff0c\u5219\u7531Bootstrap ClassLoader\u51fa\u9762\uff0c\u5b83\u9996\u5148\u67e5\u627e\u7f13\u5b58\u3002\u5982\u679c\u6709\u7684\u8bdd\u5219\u8fd4\u56de\uff0c\u6ca1\u6709\u7684\u8bdd\uff0c\u5219\u5f00\u59cb\u7b2c\u4e8c\u90e8\u5206\u8fc7\u7a0b\u3002<\/li>\n<\/ul>\n<p>\u5982\u679c\u4e0a\u8bc9\u8fd8\u662f\u6ca1\u6709\u627e\u5230\u5bf9\u5e94\u7684\u7c7b\uff0c\u5219\u5f00\u59cb\u7b2c\u4e8c\u90e8\u5206\u7684\u8fc7\u7a0b\uff1a<\/p>\n<ul>\n<li>\u5982\u679cBootstrap ClassLoader\u7f13\u5b58\u6ca1\u6709\u627e\u5230\u7684\u8bdd\uff0c\u5c31\u53bb\u627e\u81ea\u5df1\u7684\u89c4\u5b9a\u7684\u8def\u5f84\u4e0b\uff0c\u4e5f\u5c31\u662f<code>sun.mic.boot.class<\/code>\u4e0b\u9762\u7684\u8def\u5f84\u3002\u627e\u5230\u5c31\u8fd4\u56de\uff0c\u6ca1\u6709\u627e\u5230\uff0c\u8ba9\u5b50\u52a0\u8f7d\u5668\u81ea\u5df1\u53bb\u627e\u3002<\/li>\n<li>Bootstrap ClassLoader\u5982\u679c\u6ca1\u6709\u67e5\u627e\u6210\u529f\uff0c\u5219ExtClassLoader\u81ea\u5df1\u5728<code>java.ext.dirs<\/code>\u8def\u5f84\u4e2d\u53bb\u67e5\u627e\uff0c\u67e5\u627e\u6210\u529f\u5c31\u8fd4\u56de\uff0c\u67e5\u627e\u4e0d\u6210\u529f\uff0c\u518d\u5411\u4e0b\u8ba9\u5b50\u52a0\u8f7d\u5668\u627e\u3002<\/li>\n<li>ExtClassLoader\u67e5\u627e\u4e0d\u6210\u529f\uff0cAppClassLoader\u5c31\u81ea\u5df1\u67e5\u627e\uff0c\u5728<code>java.class.path<\/code>\u8def\u5f84\u4e0b\u67e5\u627e\u3002\u627e\u5230\u5c31\u8fd4\u56de\u3002\u5982\u679c\u6ca1\u6709\u627e\u5230\u5c31\u8ba9\u5b50\u7c7b\u627e\uff0c\u5982\u679c\u6ca1\u6709\u5b50\u7c7b\u4f1a\u600e\u4e48\u6837\uff1f\u629b\u51fa\u5404\u79cd\u5f02\u5e38\u3002<\/li>\n<\/ul>\n<h5>\u51e0\u4e2a\u91cd\u8981\u65b9\u6cd5\uff1a<\/h5>\n<p>\u4e0a\u9762\u5df2\u7ecf\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u52a0\u8f7d\u8fc7\u7a0b\uff0c\u4f46\u5177\u4f53\u4e3a\u4ec0\u4e48\u662f\u8fd9\u6837\u52a0\u8f7d\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u4e86\u89e3\u51e0\u4e2a\u4e2a\u91cd\u8981\u7684\u65b9\u6cd5loadClass()\u3001findLoadedClass()\u3001findClass()\u3001defineClass()\u3002<\/p>\n<p>loadclass\u65b9\u6cd5\u539f\u578b\uff1a<\/p>\n<pre><code>protected Class&lt;?&gt; loadClass(String name,\n                             boolean resolve)\n                      throws ClassNotFoundException<\/code><\/pre>\n<p>\u65b9\u6cd5\u7684\u6267\u884c\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li>\u6267\u884c<code>findLoadedClass(String)<\/code>\u53bb\u68c0\u6d4b\u8fd9\u4e2aclass\u662f\u4e0d\u662f\u5df2\u7ecf\u52a0\u8f7d\u8fc7\u4e86\u3002<\/li>\n<li>\u6267\u884c\u7236\u52a0\u8f7d\u5668\u7684<code>loadClass<\/code>\u65b9\u6cd5\u3002\u5982\u679c\u7236\u52a0\u8f7d\u5668\u4e3anull\uff0c\u5219jvm\u5185\u7f6e\u7684\u52a0\u8f7d\u5668\u53bb\u66ff\u4ee3\uff0c\u4e5f\u5c31\u662fBootstrap ClassLoader\u3002\u8fd9\u4e5f\u89e3\u91ca\u4e86ExtClassLoader\u7684parent\u4e3anull,\u4f46\u4ecd\u7136\u8bf4Bootstrap ClassLoader\u662f\u5b83\u7684\u7236\u52a0\u8f7d\u5668\u3002<\/li>\n<li>\u5982\u679c\u5411\u4e0a\u59d4\u6258\u7236\u52a0\u8f7d\u5668\u6ca1\u6709\u52a0\u8f7d\u6210\u529f\uff0c\u5219\u901a\u8fc7<code>findClass(String)<\/code>\u67e5\u627e\u3002<\/li>\n<li>findClass\u6839\u636e\u7c7b\u540d\uff0c\u53bb\u7279\u5b9a\u7684\u4f4d\u7f6e\uff08\u6587\u4ef6\u7cfb\u7edf\u3001\u7f51\u7edc\u3001\u6570\u636e\u5e93\u3001\u52a0\u5bc6\u7a7a\u95f4\uff09\u627e\u5230\u7c7b\u7684<strong>\u4e8c\u8fdb\u5236\u5b57\u8282\u6570\u7ec4 (<code>byte[]<\/code>)<\/strong>\u3002<\/li>\n<li>\u62ff\u5230\u5b57\u8282\u6570\u7ec4\u540e\uff0c\u8c03\u7528 <code>defineClass()<\/code> \u5c06\u5176\u8f6c\u6362\u4e3a <code>Class<\/code> \u5bf9\u8c61\u3002<\/li>\n<\/ol>\n<pre><code>\u6d41\u7a0b\u56fe\uff1a\nsequenceDiagram\n    participant User as \u7528\u6237\u4ee3\u7801\n    participant Load as loadClass()\n    participant Cache as findLoadedClass()\n    participant Parent as \u7236\u52a0\u8f7d\u5668\n    participant Find as findClass() (\u91cd\u5199)\n    participant Define as defineClass() (JVM)\n\n    User-&gt;&gt;Load: loadClass(\"com.example.Secret\")\n\n    Load-&gt;&gt;Cache: \u68c0\u67e5\u7f13\u5b58 (\"Secret\" \u52a0\u8f7d\u8fc7\u5417\uff1f)\n    alt \u5df2\u52a0\u8f7d\n        Cache--&gt;&gt;Load: \u8fd4\u56de Class \u5bf9\u8c61\n        Load--&gt;&gt;User: \u8fd4\u56de Class \u5bf9\u8c61\n    else \u672a\u52a0\u8f7d\n        Cache--&gt;&gt;Load: \u8fd4\u56de null\n\n        Load-&gt;&gt;Parent: \u59d4\u6258\u7236\u7ea7\u52a0\u8f7d\n        alt \u7236\u7ea7\u627e\u5230\n            Parent--&gt;&gt;Load: \u8fd4\u56de Class \u5bf9\u8c61\n            Load--&gt;&gt;User: \u8fd4\u56de Class \u5bf9\u8c61\n        else \u7236\u7ea7\u627e\u4e0d\u5230\n            Parent--&gt;&gt;Load: \u629b\u51fa\u5f02\u5e38\/\u8fd4\u56de null\n\n            Note over Load, Find: \u7236\u7ea7\u4e0d\u884c\uff0c\u81ea\u5df1\u4e0a\uff01\n            Load-&gt;&gt;Find: \u8c03\u7528 findClass(\"Secret\")\n\n            Note right of Find: 1. \u8bfb\u53d6\u52a0\u5bc6\u6587\u4ef6&lt;br\/&gt;2. \u5f02\u6216\u89e3\u5bc6&lt;br\/&gt;3. \u5f97\u5230 byte[]\n\n            Find-&gt;&gt;Define: defineClass(name, byte[], ...)\n\n            Note right of Define: JVM \u9a8c\u8bc1\u5b57\u8282\u7801&lt;br\/&gt;\u751f\u6210 Class \u5bf9\u8c61\n\n            Define--&gt;&gt;Find: \u8fd4\u56de Class \u5bf9\u8c61\n            Find--&gt;&gt;Load: \u8fd4\u56de Class \u5bf9\u8c61\n            Load--&gt;&gt;User: \u8fd4\u56de Class \u5bf9\u8c61\n        end\n    end<\/code><\/pre>\n<p>\u5982\u679cclass\u5728\u4e0a\u9762\u7684\u6b65\u9aa4\u4e2d\u627e\u5230\u4e86\uff0c\u53c2\u6570resolve\u53c8\u662ftrue\u7684\u8bdd\uff0c\u90a3\u4e48<code>loadClass()<\/code>\u53c8\u4f1a\u8c03\u7528<code>resolveClass(Class)<\/code>\u8fd9\u4e2a\u65b9\u6cd5\u6765\u751f\u6210\u6700\u7ec8\u7684Class\u5bf9\u8c61\u3002 \u6211\u4eec\u53ef\u4ee5\u4ece\u6e90\u4ee3\u7801\u770b\u51fa\u8fd9\u4e2a\u6b65\u9aa4\u3002<\/p>\n<pre><code>protected Class&lt;?&gt; loadClass(String name, boolean resolve)\n        throws ClassNotFoundException\n    {\n        synchronized (getClassLoadingLock(name)) {\n            \/\/ \u9996\u5148\uff0c\u68c0\u6d4b\u662f\u5426\u5df2\u7ecf\u52a0\u8f7d\n            Class&lt;?&gt; c = findLoadedClass(name);\n            if (c == null) {\n                long t0 = System.nanoTime();\n                try {\n                    if (parent != null) {\n                        \/\/\u7236\u52a0\u8f7d\u5668\u4e0d\u4e3a\u7a7a\u5219\u8c03\u7528\u7236\u52a0\u8f7d\u5668\u7684loadClass\n                        c = parent.loadClass(name, false);\n                    } else {\n                        \/\/\u7236\u52a0\u8f7d\u5668\u4e3a\u7a7a\u5219\u8c03\u7528Bootstrap Classloader\n                        c = findBootstrapClassOrNull(name);\n                    }\n                } catch (ClassNotFoundException e) {\n                    \/\/ ClassNotFoundException thrown if class not found\n                    \/\/ from the non-null parent class loader\n                }\n\n                if (c == null) {\n                    \/\/ If still not found, then invoke findClass in order\n                    \/\/ to find the class.\n                    long t1 = System.nanoTime();\n                    \/\/\u7236\u52a0\u8f7d\u5668\u6ca1\u6709\u627e\u5230\uff0c\u5219\u8c03\u7528findclass\n                    c = findClass(name);\n\n                    \/\/ this is the defining class loader; record the stats\n                    sun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0);\n                    sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);\n                    sun.misc.PerfCounter.getFindClasses().increment();\n                }\n            }\n            if (resolve) {\n                \/\/\u8c03\u7528resolveClass()\n                resolveClass(c);\n            }\n            return c;\n        }\n    }<\/code><\/pre>\n<p>\u4ee3\u7801\u89e3\u91ca\u4e86\u53cc\u4eb2\u59d4\u6258\u3002<\/p>\n<h3>\u81ea\u5b9a\u4e49ClassLoader\uff1a<\/h3>\n<h5>\u6b65\u9aa4\uff1a<\/h5>\n<ol>\n<li>\u7f16\u5199\u4e00\u4e2a\u7c7b\u7ee7\u627f\u81eaClassLoader\u62bd\u8c61\u7c7b\u3002<\/li>\n<li>\u590d\u5199\u5b83\u7684<code>findClass()<\/code>\u65b9\u6cd5\u3002<\/li>\n<li>\u5728<code>findClass()<\/code>\u65b9\u6cd5\u4e2d\u8c03\u7528<code>defineClass()<\/code>\u3002<\/li>\n<\/ol>\n<p>\u6ce8\u610f\uff1a<strong>\u4e00\u4e2aClassLoader\u521b\u5efa\u65f6\u5982\u679c\u6ca1\u6709\u6307\u5b9aparent\uff0c\u90a3\u4e48\u5b83\u7684parent\u9ed8\u8ba4\u5c31\u662fAppClassLoader\u3002<\/strong><\/p>\n<p>\u4f8b\uff1a<\/p>\n<pre><code>import java.io.ByteArrayOutputStream;\nimport java.io.File;\nimport java.io.FileInputStream;\nimport java.io.FileNotFoundException;\nimport java.io.IOException;\n\npublic class DiskClassLoader extends ClassLoader {\n\n    private String mLibPath;\n\n    public DiskClassLoader(String path) {\n        \/\/ TODO Auto-generated constructor stub\n        mLibPath = path;\n    }\n    @Override\n    protected Class&lt;?&gt; findClass(String name) throws ClassNotFoundException {\n        \/\/ TODO Auto-generated method stub\n        String fileName = getFileName(name);\n        File file = new File(mLibPath,fileName);\n        try {\n            FileInputStream is = new FileInputStream(file);\n            ByteArrayOutputStream bos = new ByteArrayOutputStream();\n            int len = 0;\n            try {\n                while ((len = is.read()) != -1) {\n                    bos.write(len);\n                }\n            } catch (IOException e) {\n                e.printStackTrace();\n            }\n            byte[] data = bos.toByteArray();\n            is.close();\n            bos.close();\n            return defineClass(name,data,0,data.length);\n        } catch (IOException e) {\n            \/\/ TODO Auto-generated catch block\n            e.printStackTrace();\n        }\n        return super.findClass(name);\n    }\n    \/\/\u83b7\u53d6\u8981\u52a0\u8f7d \u7684class\u6587\u4ef6\u540d\n    private String getFileName(String name) {\n        \/\/ TODO Auto-generated method stub\n        int index = name.lastIndexOf('.');\n        if(index == -1){ \n            return name+\".class\";\n        }else{\n            return name.substring(index+1)+\".class\";\n        }\n    }\n}<\/code><\/pre>\n<p>\u6ce8\u610f\uff1a\u5728JDK1.2\u4e4b\u524d\uff0c\u7c7b\u52a0\u8f7d\u5c1a\u672a\u5f15\u5165\u53cc\u4eb2\u59d4\u6d3e\u6a21\u5f0f\uff0c\u56e0\u6b64\u5b9e\u73b0\u81ea\u5b9a\u4e49\u7c7b\u52a0\u8f7d\u5668\u65f6\u5e38\u5e38\u91cd\u5199loadClass\u65b9\u6cd5\uff0c\u63d0\u4f9b\u53cc\u4eb2\u59d4\u6d3e\u903b\u8f91\uff0c\u4eceJDK1.2\u4e4b\u540e\uff0c\u53cc\u4eb2\u59d4\u6d3e\u6a21\u5f0f\u5df2\u7ecf\u88ab\u5f15\u5165\u5230\u7c7b\u52a0\u8f7d\u4f53\u7cfb\u4e2d\uff0c\u81ea\u5b9a\u4e49\u7c7b\u52a0\u8f7d\u5668\u65f6\u4e0d\u9700\u8981\u5728\u81ea\u5df1\u5199\u53cc\u4eb2\u59d4\u6d3e\u7684\u903b\u8f91\uff0c\u56e0\u6b64\u4e0d\u9f13\u52b1\u91cd\u5199loadClass\u65b9\u6cd5\uff0c\u800c\u63a8\u8350\u91cd\u5199findClass\u65b9\u6cd5\u3002<\/p>\n<h5>\u57fa\u4e8e\u81ea\u5b9a\u4e49\u7684\u4e00\u4e9b\u82b1\u6d3b\uff1a<\/h5>\n<p>\u7f16\u5199\u52a0\u5bc6\u5de5\u5177\uff1a<\/p>\n<pre><code>import java.io.File;\nimport java.io.FileInputStream;\nimport java.io.FileNotFoundException;\nimport java.io.FileOutputStream;\nimport java.io.IOException;\npublic class FileUtils {\n    public static void test(String path){\n        File file = new File(path);\n        try {\n            FileInputStream fis = new FileInputStream(file);\n            FileOutputStream fos = new FileOutputStream(path+\"en\");\n            int b = 0;\n            int b1 = 0;\n            try {\n                while((b = fis.read()) != -1){\n                    \/\/\u6bcf\u4e00\u4e2abyte\u5f02\u6216\u4e00\u4e2a\u6570\u5b572\n                    fos.write(b ^ 2);\n                }\n                fos.close();\n                fis.close();\n            } catch (IOException e) {\n                \/\/ TODO Auto-generated catch block\n                e.printStackTrace();\n            }\n        } catch (FileNotFoundException e) {\n            \/\/ TODO Auto-generated catch block\n            e.printStackTrace();\n        }\n    }\n}<\/code><\/pre>\n<p>\u7136\u540e\u81ea\u5b9a\u4e49\u7684\u89e3\u5bc6\u7c7b\u52a0\u8f7d\u5668\uff1a<\/p>\n<pre><code>import java.io.ByteArrayOutputStream;\nimport java.io.File;\nimport java.io.FileInputStream;\nimport java.io.IOException;\npublic class DeClassLoader extends ClassLoader {\n    private String mLibPath;\n    public DeClassLoader(String path) {\n        \/\/ TODO Auto-generated constructor stub\n        mLibPath = path;\n    }\n    @Override\n    protected Class&lt;?&gt; findClass(String name) throws ClassNotFoundException {\n        \/\/ TODO Auto-generated method stub\n        String fileName = getFileName(name);\n        File file = new File(mLibPath,fileName);\n        try {\n            FileInputStream is = new FileInputStream(file);\n            ByteArrayOutputStream bos = new ByteArrayOutputStream();\n            int len = 0;\n            byte b = 0;\n            try {\n                while ((len = is.read()) != -1) {\n                    \/\/\u5c06\u6570\u636e\u5f02\u6216\u4e00\u4e2a\u6570\u5b572\u8fdb\u884c\u89e3\u5bc6\n                    b = (byte) (len ^ 2);\n                    bos.write(b);\n                }\n            } catch (IOException e) {\n                e.printStackTrace();\n            }\n            byte[] data = bos.toByteArray();\n            is.close();\n            bos.close();\n            return defineClass(name,data,0,data.length);\n        } catch (IOException e) {\n            \/\/ TODO Auto-generated catch block\n            e.printStackTrace();\n        }\n        return super.findClass(name);\n    }\n    \/\/\u83b7\u53d6\u8981\u52a0\u8f7d \u7684class\u6587\u4ef6\u540d\n    private String getFileName(String name) {\n        \/\/ TODO Auto-generated method stub\n        int index = name.lastIndexOf('.');\n        if(index == -1){ \n            return name+\".classen\";\n        }else{\n            return name.substring(index+1)+\".classen\";\n        }\n    }\n}<\/code><\/pre>\n<h3>Context ClassLoader \u7ebf\u7a0b\u4e0a\u4e0b\u6587\u7c7b\u52a0\u8f7d\u5668\uff1a<\/h3>\n<p>ContextClassLoader\u5176\u5b9e\u53ea\u662f\u4e00\u4e2a\u6982\u5ff5\u3002<\/p>\n<p>\u6e90\u7801\uff1a<\/p>\n<pre><code>public class Thread implements Runnable {\n\n\/* The context ClassLoader for this thread *\/\n   private ClassLoader contextClassLoader;\n\n   public void setContextClassLoader(ClassLoader cl) {\n       SecurityManager sm = System.getSecurityManager();\n       if (sm != null) {\n           sm.checkPermission(new RuntimePermission(\"setContextClassLoader\"));\n       }\n       contextClassLoader = cl;\n   }\n\n   public ClassLoader getContextClassLoader() {\n       if (contextClassLoader == null)\n           return null;\n       SecurityManager sm = System.getSecurityManager();\n       if (sm != null) {\n           ClassLoader.checkClassLoaderPermission(contextClassLoader,\n                                                  Reflection.getCallerClass());\n       }\n       return contextClassLoader;\n   }\n}<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\u5b83\u53ea\u662f\u4ee3\u8868\u4e00\u4e2aclassloader\u7684\u53d8\u91cf\u800c\u5df2\u3002<\/p>\n<p>\u6bcf\u4e2aThread\uff08\u7ebf\u7a0b\uff09\u90fd\u6709\u4e00\u4e2a\u76f8\u5173\u8054\u7684ClassLoader\uff0c\u9ed8\u8ba4\u662fAppClassLoader\u3002\u5e76\u4e14\u5b50\u7ebf\u7a0b\u9ed8\u8ba4\u4f7f\u7528\u7236\u7ebf\u7a0b\u7684ClassLoader\u9664\u975e\u5b50\u7ebf\u7a0b\u7279\u522b\u8bbe\u7f6e\u3002<\/p>\n<p>\u7528\u9014\uff1a\u8ba9\u7236\u52a0\u8f7d\u5668\u80fd\u591f\u7528\u7ebf\u7a0b\u8bbe\u5b9a\u7684\u5b50\u52a0\u8f7d\u5668\u6743\u9650\u8bbf\u95ee\u7236\u52a0\u8f7d\u5668\u627e\u4e0d\u5230\u7684\u5b50\u52a0\u8f7d\u5668\u8d44\u6e90\uff0c\u89e3\u51b3\u53cc\u4eb2\u59d4\u6d3e\u6a21\u5f0f\u4e0b\u53ea\u80fd\u5411\u4e0a\u9012\u5f52\u59d4\u6258\u7684\u7f3a\u9677<\/p>\n<p>\u4f8b\u5b50\uff1aJDK \u6838\u5fc3\u4ee3\u7801\uff08\u7236\u52a0\u8f7d\u5668\uff09\u9700\u8981\u52a0\u8f7d\u4f60\u9879\u76ee\u91cc\u7684 MySQL \u9a71\u52a8\uff08\u5b50\u52a0\u8f7d\u5668\u8d44\u6e90\uff09<\/p>\n<pre><code>\/\/ 1. JDK \u6e90\u7801 (java.sql.DriverManager)\n\/\/ \u5b83\u81ea\u5df1\u627e\u4e0d\u5230 mysql \u9a71\u52a8\uff0c\u4e8e\u662f\u201c\u501f\u7528\u201d\u5f53\u524d\u7ebf\u7a0b\u7684\u7c7b\u52a0\u8f7d\u5668\nClassLoader cl = Thread.currentThread().getContextClassLoader();\n\/\/ \u6210\u529f\u52a0\u8f7d\u4e86\u4f4d\u4e8e classpath \u4e0b\u7684 com.mysql.cj.jdbc.Driver\nClass&lt;?&gt; driver = cl.loadClass(\"com.mysql.cj.jdbc.Driver\"); \n\n\/\/ 2. \u4f60\u7684\u4ee3\u7801 (main \u65b9\u6cd5)\n\/\/ \u4e3b\u7ebf\u7a0b\u9ed8\u8ba4\u5c31\u628a AppClassLoader \u8bbe\u4e3a\u4e86\u4e0a\u4e0b\u6587\u7c7b\u52a0\u8f7d\u5668\n\/\/ \u6240\u4ee5\u4e0a\u9762\u90a3\u884c\u4ee3\u7801\u80fd\u6210\u529f\u8fd0\u884c\uff0c\u65e0\u9700\u4f60\u989d\u5916\u914d\u7f6e\nDriverManager.getConnection(\"jdbc:mysql:\/\/localhost:3306\/db\", \"user\", \"pass\");<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>classloader\uff1a JVM\u662f\u4ec0\u4e48\uff1a\u7c7b\u4f3c\u4e8e\u7ffb\u8bd1\u5668\uff0c\u5c06\u540c\u4e00\u4e2ajava\u4ee3\u7801\u7ffb\u8bd1\u7ed9\u4e0d\u540c\u7684\u64cd\u4f5c\u7cfb\u7edf\uff0c\u4ee5\u8fbe\u5230\u901a\u7528\u7684\u6548 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-33","post","type-post","status-publish","format-standard","hentry","category-3"],"_links":{"self":[{"href":"https:\/\/arknight.wiki\/index.php\/wp-json\/wp\/v2\/posts\/33","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/arknight.wiki\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/arknight.wiki\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/arknight.wiki\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/arknight.wiki\/index.php\/wp-json\/wp\/v2\/comments?post=33"}],"version-history":[{"count":1,"href":"https:\/\/arknight.wiki\/index.php\/wp-json\/wp\/v2\/posts\/33\/revisions"}],"predecessor-version":[{"id":34,"href":"https:\/\/arknight.wiki\/index.php\/wp-json\/wp\/v2\/posts\/33\/revisions\/34"}],"wp:attachment":[{"href":"https:\/\/arknight.wiki\/index.php\/wp-json\/wp\/v2\/media?parent=33"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/arknight.wiki\/index.php\/wp-json\/wp\/v2\/categories?post=33"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/arknight.wiki\/index.php\/wp-json\/wp\/v2\/tags?post=33"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}