{"id":21515,"date":"2017-11-29T16:09:09","date_gmt":"2017-11-29T16:09:09","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=21515"},"modified":"2024-11-16T14:40:11","modified_gmt":"2024-11-16T14:40:11","slug":"what-is-a-classloader","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/what-is-a-classloader\/","title":{"rendered":"What is a ClassLoader?"},"content":{"rendered":"<h1>Overview<\/h1>\n<p>ClassLoader is a class that is responsible to load classes into JVM\u2019s(Java Virtual Machine) method area.<\/p>\n<p>Basic class loaders\u00a0 are listed below:<\/p>\n<ul>\n<li>ApplicationClassLoader<\/li>\n<li>ExtensionClassLoader<\/li>\n<li>BootStrapClassLoader<\/li>\n<\/ul>\n<h2>ApplicationClassLoader<\/h2>\n<p>ApplicationClassLoader to load the classes from the application classpath,(current working directory). It basically uses a classpath environment variable to locate the class i.e. \u201c.class\u201d file.<\/p>\n<h2>ExtensionClassLoader<\/h2>\n<p>ExtensionClassLoader to load the classes from extension classpath,(%<strong>JAVA_HOME<\/strong>%\\jre\\lib\\ext\u201d folder).<\/p>\n<h2>BootStrapClassLoader<\/h2>\n<p>BootStrapClassLoader to load the classes from BootStrap classpath, (%<strong>JAVA_HOME<\/strong>%\\jre\\lib\\rt.jar\u201d folder). These classes are predefined classes.<\/p>\n<p>&nbsp;<\/p>\n<h2><strong>How ClassLoader works?<\/strong><\/h2>\n<p>Whenever JVM comes across a type, it checks for that class byte code in the method area. If it&#8217;s not loaded, it requests the class loader subsystem to load the corresponding class byte code in the method area from that class respective Classpath. Alternatively, if it is already loaded it makes use of that type.<\/p>\n<p>ClassLoader then handovers the request to ApplicationClassLoader, which searches for that class in the folders configured in the Classpath environment variable. If a class is not found, it forwards the request to ExtensionClassLoader and then to BootStrapClassLoader.<\/p>\n<p>If the search fails in all the ClassLoaders then then JVM throws an exception \u201cjava.lanf.NoClassDefFoundError\u201d \/ \u201cjava.lang.ClassNotFoundException\u201d.<\/p>\n<p>If class found in any one of the classpaths, the respective ClassLoader loads that class into JVM\u2019s method area.<\/p>\n<p>Please find the below figure for more clarity.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-10007\" src=\"http:\/\/www.testingdocs.com\/wp-content\/uploads\/ClassLoaders.png\" alt=\"ClassLoaders\" width=\"723\" height=\"343\" title=\"\"><\/p>\n<p>&nbsp;<\/p>\n<p>&#8212;<\/p>\n<h2>Java Tutorials<\/h2>\n<p>Java Tutorial on this website:<\/p>\n<p><a href=\"https:\/\/www.testingdocs.com\/java-tutorial\/\">https:\/\/www.testingdocs.com\/java-tutorial\/<\/a><\/p>\n<p>For more information on Java, visit the official website :<\/p>\n<p><a href=\"https:\/\/www.oracle.com\/in\/java\/\" rel=\"noopener\">https:\/\/www.oracle.com\/in\/java\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Overview ClassLoader is a class that is responsible to load classes into JVM\u2019s(Java Virtual Machine) method area. Basic class loaders\u00a0 are listed below: ApplicationClassLoader ExtensionClassLoader BootStrapClassLoader ApplicationClassLoader ApplicationClassLoader to load the classes from the application classpath,(current working directory). It basically uses a classpath environment variable to locate the class i.e. \u201c.class\u201d file. ExtensionClassLoader ExtensionClassLoader to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[],"class_list":["post-21515","post","type-post","status-publish","format-standard","hentry","category-java","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/21515","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=21515"}],"version-history":[{"count":6,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/21515\/revisions"}],"predecessor-version":[{"id":23224,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/21515\/revisions\/23224"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=21515"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=21515"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=21515"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}