{"id":381,"date":"2007-10-21T00:00:00","date_gmt":"2007-10-21T00:00:00","guid":{"rendered":"http:\/\/www.strongd.net\/?p=381"},"modified":"2007-10-21T00:00:00","modified_gmt":"2007-10-21T00:00:00","slug":"Understanding Extension Class Loading","status":"publish","type":"post","link":"https:\/\/www.strongd.net\/?p=381","title":{"rendered":"Understanding Extension Class Loading"},"content":{"rendered":"<p><DIV id=PageTitle>Understanding Extension Class Loading<\/DIV><br \/>\n<BLOCKQUOTE>The extension framework makes use of the class-loading delegation mechanism. When the runtime environment needs to load a new class for an application, it looks for the class in the following locations, in order:<br \/>\n<OL><br \/>\n<LI><B>Bootstrap classes<\/B>: the runtime classes in <TT>rt.jar<\/TT>, internationalization classes in <TT>i18n.jar<\/TT>, and others. <\/LI><br \/>\n<LI><B>Installed extensions<\/B>: classes in JAR files in the <TT>lib\/ext<\/TT> directory of the JRE, and in the system-wide, platform-specific extension directory (such as <TT>\/usr\/jdk\/packages\/lib\/ext<\/TT> on the Solaris<FONT size=-2><SUP>TM<\/SUP><\/FONT> Operating System, but note that use of this directory applies only to Java<FONT size=-2><SUP>TM<\/SUP><\/FONT> 6 and later). <\/LI><br \/>\n<LI><B>The class path<\/B>: classes, including classes in JAR files, on paths specified by the system property <TT>java.class.path<\/TT>. If a JAR file on the class path has a manifest with the <CODE>Class-Path<\/CODE> attribute, JAR files specified by the <CODE>Class-Path<\/CODE> attribute will be searched also. By default, the <CODE>java.class.path<\/CODE> property&#8217;s value is <CODE>.<\/CODE>, the current directory. You can change the value by using the <TT>-classpath<\/TT> or <TT>-cp<\/TT> command-line options, or setting the <CODE>CLASSPATH<\/CODE> environment variable. The command-line options override the setting of the <CODE>CLASSPATH<\/CODE> environment variable. <\/LI><\/OL><br \/>\n<P>The precedence list tells you, for example, that the class path is searched only if a class to be loaded hasn&#8217;t been found among the classes in <TT>rt.jar<\/TT>, <TT>i18n.jar<\/TT> or the installed extensions. <\/P><br \/>\n<P>Unless your software instantiates its own class loaders for special purposes, you don&#8217;t really need to know much more than to keep this precedence list in mind. In particular, you should be aware of any class name conflicts that might be present. For example, if you list a class on the class path, you&#8217;ll get unexpected results if the runtime environment instead loads another class of the same name that it found in an installed extension. <\/P><\/BLOCKQUOTE><br \/>\n<H3>The Java Class Loading Mechanism<\/H3><br \/>\n<BLOCKQUOTE>The Java platform uses a delegation model for loading classes. The basic idea is that every class loader has a &#8220;parent&#8221; class loader. When loading a class, a class loader first &#8220;delegates&#8221; the search for the class to its parent class loader before attempting to find the class itself.<br \/>\n<P>Here are some highlights of the class-loading API: <\/P><br \/>\n<UL><br \/>\n<LI>Constructors in <TT>java.lang.ClassLoader<\/TT> and its subclasses allow you to specify a parent when you instantiate a new class loader. If you don&#8217;t explicitly specify a parent, the virtual machine&#8217;s system class loader will be assigned as the default parent. <\/LI><br \/>\n<LI>The <TT>loadClass<\/TT> method in <TT>ClassLoader<\/TT> performs these tasks, in order, when called to load a class:<br \/>\n<OL><br \/>\n<LI>If a class has already been loaded, it returns it. <\/LI><br \/>\n<LI>Otherwise, it delegates the search for the new class to the parent class loader. <\/LI><br \/>\n<LI>If the parent class loader does not find the class, <TT>loadClass<\/TT> calls the method <TT>findClass<\/TT> to find and load the class. <\/LI><\/OL><\/LI><br \/>\n<LI>The <TT>findClass<\/TT> method of <TT>ClassLoader<\/TT> searches for the class in the current class loader if the class wasn&#8217;t found by the parent class loader. You will probably want to override this method when you instantiate a class loader subclass in your application. <\/LI><br \/>\n<LI>The class <TT>java.net.URLClassLoader<\/TT> serves as the basic class loader for extensions and other JAR files, overriding the <TT>findClass<\/TT> method of <TT>java.lang.ClassLoader<\/TT> to search one or more specified URLs for classes and resources. <\/LI><\/UL><br \/>\n<P>To see a sample application that uses some of the API as it relates to JAR files, see the <A class=TutorialLink href=\"http:\/\/www.strongd.net\/blog\/show\/373\" target=_top>Using JAR-related APIs<\/A> lesson in this tutorial. <\/P><\/BLOCKQUOTE><br \/>\n<H3>Class Loading and the <TT>java<\/TT> Command<\/H3><br \/>\n<BLOCKQUOTE>The Java platform&#8217;s class-loading mechanism is reflected in the <TT>java<\/TT> command.<br \/>\n<UL><br \/>\n<LI>In the <TT>java<\/TT> tool, the <TT>-classpath<\/TT> option is a shorthand way to set the <TT>java.class.path<\/TT> property. <\/LI><br \/>\n<LI>The <TT>-cp<\/TT> and <TT>-classpath<\/TT> options are equivalent. <\/LI><br \/>\n<LI>The <TT>-jar<\/TT> option runs applications that are packaged in JAR files. For a description and examples of this option, see the <A class=TutorialLink href=\"http:\/\/www.strongd.net\/blog\/show\/369\" target=_top>Running JAR-Packaged Software<\/A> lesson in this tutorial. <\/LI><\/UL><\/BLOCKQUOTE><br \/>\n<DIV><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Understanding Extension Class Loading The extension framework makes use of the class-loading delegation mechanism. When the runtime environment needs to load a new class for an application, it looks for the class in the following locations, in order: Bootstrap classes: the runtime classes in rt.jar, internationalization classes in i18n.jar, and others. Installed extensions: classes in &hellip; <a href=\"https:\/\/www.strongd.net\/?p=381\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Understanding Extension Class Loading<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-381","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/381","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=381"}],"version-history":[{"count":0,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/381\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=381"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=381"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=381"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}