<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>TechJava &#187; TechJava &#8211; Articles tagged by class loading</title>
	<atom:link href="http://www.techjava.de/topics/tag/class-loading/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.techjava.de</link>
	<description>Journal on Java Technology</description>
	<lastBuildDate>Wed, 11 May 2011 21:15:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>The PluginLoader &#8211; Applying Java Class loading Concepts</title>
		<link>http://www.techjava.de/topics/2008/02/plugin-class-loader/</link>
		<comments>http://www.techjava.de/topics/2008/02/plugin-class-loader/#comments</comments>
		<pubDate>Mon, 25 Feb 2008 08:29:16 +0000</pubDate>
		<dc:creator>joker</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[class loader]]></category>
		<category><![CDATA[class loading]]></category>
		<category><![CDATA[component]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[JAR]]></category>
		<category><![CDATA[JVM]]></category>
		<category><![CDATA[library]]></category>
		<category><![CDATA[plugin]]></category>

		<guid isPermaLink="false">http://www.techjava.de/topics/2008/02/plugin-class-loader/</guid>
		<description><![CDATA[Introduction Many frameworks support plugins for increasing flexibility. They need to be loaded during runtime making it possible to change the supported features used in application without recompiling the framework and application themselves. Loading a plug-in means loading java classes, which is done by a class loader. We want to load the plug-ins from jar [...]]]></description>
			<content:encoded><![CDATA[<h3>Introduction</h3>
<p>Many frameworks support plugins for increasing flexibility. They need to be loaded during runtime making it possible to change the supported features used in application without recompiling the framework and application themselves. Loading a plug-in means loading java classes, which is done by a class loader. We want to load the plug-ins from jar files, without providing a name for every JAR. Unfortunately, the java standard API does not contain 
<a  href="http://www.techjava.de/topics/tag/class-loading/">a class loader</a> for loading classes from a list of jar files in a directory. We need to provide it ourselves. In addition, the standard class loading mechanism is designed with following idea in mind: in runtime the application accesses the known classname, that is searched for in class path and loaded if found. In our case, we want to force the framework load it extensions, without setting up the class path before applications starts, which is a little different use case.</p>
<h3>Simple JAR class loader</h3>
<p>The first step is loading classes from jar files. For this purpose we create a class named <strong>JarFileClassLoader</strong> which should be able to load a class from a jar file. It inherits from <strong>ClassLoader</strong>, the Java default implementation for a class loader. Loading a class from a jar file means scanning each jar file entries, decide if it is a class then load it. Because we want to be flexible about the criterion we use to select our classes, we introduce an interface <strong>ICriterion</strong>, which decides for a given class if the criterion is fulfilled or not. Using this, the JarFileClassLoader can now scan a single jar file and load every class fulfilling the injected criterion. Loading a class means scanning the caches of the parent class loaders, calling the <strong>loadClass(String)</strong> method of the parents if not found and scanning the jar file afterwards if the class was still not found. This beahviour is described in a previous 
<a  href="http://www.techjava.de/topics/tag/class-loading/">post</a>, introducing the basics of Java Class Loading. </p>
<p>JarFileClassLoader is capable of loading a class if it does not reference other classes. These references have to be resolved during class loading time. Let consider the example of classes A and B where A references B. If A is loaded by a class loader CL, CL is asked for loading B also. This is done by calling the <strong>loadClass(String)</strong> method of CL. If B is contained in the jar file CL loads classes from, CL will find it and return it to the JVM. If B is not contained in CL’s jar file, CL’s parents are asked to load it. Thus the class will be loaded if it is contained in the classpath where one of the java class loaders will find it. If the class is contained in another jar file in the list, but not in the class path it will not be found and the loading will end with a <strong>ClassNotFoundException</strong>.<br />
<img src="http://www.techjava.de/wp-content/uploads/PluginClassLoader/pluginclassloader.png" alt="Plugin Class Loader" style="float:right; margin:5px" /></p>
<h3>Plugin class loader</h3>
<p>To tackle this problem we introduce another class, the <strong>PluginLoader</strong>. It contains a collection of <strong>JarFileClassLoaders</strong> and inherits from ClassLoader, thus is a ClassLoader itself. The <strong>PluginLoader</strong> acts as a parent for all <strong>JarFileClassLoaders</strong> and is therefore asked every time a class has to be loaded. It will then iterate over all <strong>JarFileClassLoaders</strong> calling the <strong>loadClass(String)</strong> method on all of them to find the class it looks for. This way, classes contained in sibling class loaders are found also without violating the rule that a class loader can only see classes of itself or its parents. </p>
<h3>Putting everything together</h3>
<p>Now the structure of our plug-in loader component becomes clear. What we additionally need to do, is get rid of all the stack overflows we produce with the above classes. The problem here is that any <strong>JarFileClassLoader</strong> asks its parent, the <strong>PluginLoader</strong>, to load a class. This will ask all child <strong>JarFileClassLoaders</strong> to load the class including the CL which asked the <strong>PluginLoader</strong> before. The result is an infinite call stack loop &#8211; a stack overflow. Therefore we relax the concept of the java class loading. We introduce a method <strong>loadClassSimple(String)</strong> on the <strong>JarFileClassLoaders</strong> which looks just in its very own jar file. If it does not find the wanted class it just throws an Exception without asking a parent or cache-lookups. This method is called by the <strong>PluginLoader</strong> only, therefore the rest of the class loading is not affected.<br />
<img src="http://www.techjava.de/wp-content/uploads/PluginClassLoader/pluginclassloader_instance.png" alt="Plugin Class Loader at runtime" style="float:right; margin:5px" /><br />
Last but not least we consider some performance issues. If a class is searched in other jar files, those have to be scanned every time again resulting in poor performance. Therefore we introduce a cache in the <strong>JarFileClassLoader</strong> containing every class that was loaded before. Every request is served now by trying to find the class in the cache first. </p>
<h3>Limitations</h3>
<p>Due to the fact, that <strong>PluginLoader</strong> asks its child <strong>JarClassLoaders</strong> to load a class in particuliar order, and the first class found will be taken, the order in which <strong>JarClassLoaders</strong> are asked matters. This issue can cause problems if e.G. different versions of the same library are deployed on location scanned by the <strong>PluginLoader</strong> and should be handled with care.</p>
<h3>References</h3>
<ul>
<li>
<div>[2007,book] 
<a  href="#2007_KRUEGER" class="toggle">bibtex</a>  
<a  href='http://www.javabuch.de/} publisher =	{Addison-Wesley} isbn =	{3-8273-2373-8' title='Go to document' onclick="javascript:pageTracker._trackPageview('/external/www.javabuch.de/} publisher =_{Addison-Wesley} isbn =_{3-8273-2373-8');" ><img src='http://www.techjava.de/wp-content/plugins/bib2html/external.png' width='10' height='10' alt='Go to document' /></a></div>
<div>G. Krueger and T. Stark, <em>Handbuch der Java-Programmierung</em>, , 2007.</div>
<div class="bibtex" id="2007_KRUEGER">
         <code>@book{2007_KRUEGER, <br />
 &nbsp;&nbsp;author =	{Guido Krueger and Thomas Stark}, <br />
 &nbsp; title =	{Handbuch der Java-Programmierung}, <br />
 &nbsp; month =	Nov, year =	{2007}, <br />
 &nbsp; url = {http://www.javabuch.de/} publisher =	{Addison-Wesley} isbn =	{3-8273-2373-8}<br />
}</code>
    </div>
</li>
<li>
<div>[,techreport] 
<a  href="#" class="toggle">bibtex</a>  
<a  href='{http://www.developer.com/java/other/article.php/2248831' title='Go to document' onclick="javascript:pageTracker._trackPageview('/external/{http//www.developer.com/java/other/article.php/2248831');" ><img src='http://www.techjava.de/wp-content/plugins/bib2html/external.png' width='10' height='10' alt='Go to document' /></a></div>
<div>&quot;Java Class Loading: The Basics,&quot;.</div>
<div class="bibtex" id="">
         <code>@techreport{CL_BASICS author =	{Brandon E. Taylor}, <br />
 &nbsp; title =	{Java Class Loading: The Basics}, <br />
 &nbsp; url = {http://www.developer.com/java/other/article.php/2248831}</code>
    </div>
</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.techjava.de/topics/2008/02/plugin-class-loader/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java Class Loading</title>
		<link>http://www.techjava.de/topics/2008/01/java-class-loading/</link>
		<comments>http://www.techjava.de/topics/2008/01/java-class-loading/#comments</comments>
		<pubDate>Sat, 05 Jan 2008 13:35:21 +0000</pubDate>
		<dc:creator>shuron</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[class loading]]></category>

		<guid isPermaLink="false">http://www.techjava.de/topics/2008/01/java-class-loading/</guid>
		<description><![CDATA[It&#8217;s good to know, how Java class loading works, when you have to develop in Java. Basic understanding of class loading process helps every Java developer to deal with several ClassLoader related Exceptions. Class loader delegation The loading of Java classes is performed by class loaders (CL), they are responsible for loading classes into the [...]]]></description>
			<content:encoded><![CDATA[<p><em>It&#8217;s good to know, how Java class loading works, when you have to develop in Java. Basic understanding of class loading process helps every Java developer to deal with several ClassLoader related Exceptions.</em></p>
<h3>Class loader delegation</h3>
<p>The loading of Java classes is performed by <em>class loaders</em> (CL), they are responsible for loading classes into the JVM. Simple applications can use the Java platform&#8217;s built-in class loading facility to load their classes, more complex applications tend to define their own custom class loaders.</p>
<p>The class loaders in Java are organized in a tree. By request a class loader determines if the class has already been loaded in the past, looking up in its own cache. If the class is present in the cache the CL returns the class, if not, it delegates the request to the parent. If the parent is not set (is <code>Null</code>) or can not load the class and throws a <code>ClassNotFoundException</code> the classloader tries to load the class itself and searches its own path for the class file. If the class can be loaded it is returned, otherwise a <code>ClassNotFoundException </code>is thrown. The cache lookup goes on recursively from child to parent, until the tree root is reached or a class is found in cache. If the root is reached the class loaders try to load the class and unfold the recursion from parent to child. Summarizing that we have following order:</p>
<ul>
<li>Cache</li>
<li>Parent</li>
<li>Self</li>
</ul>
<p>This mechanism ensures that <strong>classes tending to be loaded by  class loaders nearest to the root</strong>. Remember, that parent class loader is always has the opportunity to load a class first. It is important to ensure that core Java classes are loaded by the bootstrap loader, which <strong>guarantees that the correct versions of classes such as <code>java.lang.Object</code> are loaded</strong>. Furthermore it ensures, that one class loader sees only classes loaded by itself or its parent (or further ancestors) and it cannot see classes loaded by its children or siblings!</p>
<p>The picture illustrates the hierarchy of class loaders. Root loader is <em>bootstrap</em> class loader which has native implementation and  cannot be instantiated by Java code.</p>
<p style="text-align: center"><img src="http://www.techjava.de/wp-content/uploads/JavaClassLoading/clhierarchy.gif" alt="The class loader delegation model" height="406" width="334" /></p>
<p style="text-align: center">&nbsp;</p>
<p>It is followed by <em>extension</em> class loader, which is primary responsibility to load classes from the extension directories and provides ability to simply drop in new JVM extensions, without requiring modification to the user&#8217;s classpath. The <em>system</em> or <em>application </em>class loader responsible for loading classes from the path specified by the <code>CLASSPATH</code> environment variable.  This class loader will be returned by the <code>ClassLoader.getSystemClassLoader()</code> method.</p>
<h3>Phases of class loading</h3>
<p>The are three phases of concrete class loading: <strong>physical loading</strong>, <strong>linking</strong>, and <strong>initializing</strong>.</p>
<p style="text-align: center"><img src="http://www.techjava.de/wp-content/uploads/JavaClassLoading/clphases.gif" alt="The phases of class loading" height="287" width="550" /></p>
<ol>
<li>In in first phase of <strong>physical loading</strong> required class file will be searched in specified classpaths. If the file is found it is read and the bytecode is loaded. This process gives a basic memory structure to the class object, such concepts like methods, fields, and other referenced classes are not known at this stage.</li>
<li><strong><em>Linking</em></strong> can be broken down into three main stages, because it is complex phase:
<ol>
<li><strong>Bytecode verification </strong>through class loader, which executes a number of checks on the bytecodes.</li>
<li><strong>Class preparation.</strong> This stage prepares the necessary data structures that represent fields, methods and implemented interfaces that are defined within the class.</li>
<li><strong>Resolving </strong>of all the other classes referenced by a particular class. The classes can be referenced in a number of ways:
<ul>
<li>Superclasses</li>
<li>Interfaces</li>
<li>Field types</li>
<li>Types in method signatures</li>
<li>Types of local variables used in methods</li>
</ul>
</li>
</ol>
</li>
<li>During the <strong>initializing phase</strong> any static initializers contained within a class are executed so that, static fields are initialized to their default values.</li>
</ol>
<p>It is interesting, that class loading <strong>can be performed in a lazy manner</strong> and therefore some parts of the class loading process may be done on first use of the class rather than at load time.</p>
<h3>Exceptions</h3>
<p><span id="intelliTxt"> The biggest challenge in dealing with class-loading problems is that problems rarely manifest themselves during the class-loading process but rather during the usage of a class later on</span>. In following shown two class loading related exceptions, with potential causes</p>
<ul>
<li><strong>ClassNotFoundException</strong>
<ul>
<li>An archive, directory, or other source for the classes was not added to the class loader asked to load the class, or to its parent.</li>
<li>A class loader&#8217;s parent is not set correctly.</li>
<li>The wrong class loader is used to load the class in question.</li>
</ul>
</li>
<li><strong>NoClassDefFoundError</strong>
<ul>
<li>An archive, directory, or other source for the classes was not added to the class loader asked to load the class, or to its parent.</li>
<li>A class loader&#8217;s parent is not set correctly.</li>
<li>Symbolic links in a class are unaccessible by the containing class&#8217;s class loader, such as a <em>child class loader</em>.</li>
</ul>
</li>
</ul>
<h3>References:</h3>
<ul>
<li>
<div>[2005,techreport] 
<a  href="#DW_2005_CLASSLOADING" class="toggle">bibtex</a>  
<a  href='http://www.ibm.com/developerworks/java/library/j-dclp1/index.html' title='Go to document' onclick="javascript:pageTracker._trackPageview('/external/www.ibm.com/developerworks/java/library/j-dclp1/index.html');" ><img src='http://www.techjava.de/wp-content/plugins/bib2html/external.png' width='10' height='10' alt='Go to document' /></a></div>
<div>L. Shankar and S. Burns, &quot;Demystifying class loading problems, Part 1: An introduction to class loading and debugging tools,&quot; 2005.</div>
<div class="bibtex" id="DW_2005_CLASSLOADING">
         <code>@techreport{DW_2005_CLASSLOADING, <br />
 &nbsp;&nbsp;author =	{Lakshmi Shankar and Simon Burns}, <br />
 &nbsp; title =	{Demystifying class loading problems, Part 1: An introduction to class loading and debugging tools}, <br />
 &nbsp; month =	Nov, year =	{2005}, <br />
 &nbsp; url = {http://www.ibm.com/developerworks/java/library/j-dclp1/index.html}<br />
}</code>
    </div>
</li>
</ul>
<ul>
<li>
<div>[2004,techreport] 
<a  href="#ONJAVA_2004_CLASSLOADING" class="toggle">bibtex</a>  
<a  href='http://www.onjava.com/pub/a/onjava/2004/06/30/classloader2.html' title='Go to document' onclick="javascript:pageTracker._trackPageview('/external/www.onjava.com/pub/a/onjava/2004/06/30/classloader2.html');" ><img src='http://www.techjava.de/wp-content/plugins/bib2html/external.png' width='10' height='10' alt='Go to document' /></a></div>
<div>A. Schaefer, &quot;Inside Class Loaders: Debugging,&quot; 2004.</div>
<div class="bibtex" id="ONJAVA_2004_CLASSLOADING">
         <code>@techreport{ONJAVA_2004_CLASSLOADING, <br />
 &nbsp;&nbsp;author =	{Andreas Schaefer}, <br />
 &nbsp; title =	{Inside Class Loaders: Debugging}, <br />
 &nbsp; month =	Jun, year =	{2004}, <br />
 &nbsp; url = {http://www.onjava.com/pub/a/onjava/2004/06/30/classloader2.html}<br />
}</code>
    </div>
</li>
</ul>
<ul>
<li>
<div>[2003,techreport] 
<a  href="#ONJAVA_2003_CLASSLOADING" class="toggle">bibtex</a>  
<a  href='http://www.onjava.com/pub/a/onjava/2003/11/12/classloader.html' title='Go to document' onclick="javascript:pageTracker._trackPageview('/external/www.onjava.com/pub/a/onjava/2003/11/12/classloader.html');" ><img src='http://www.techjava.de/wp-content/plugins/bib2html/external.png' width='10' height='10' alt='Go to document' /></a></div>
<div>A. Schaefer, &quot;Inside Class Loaders,&quot; 2003.</div>
<div class="bibtex" id="ONJAVA_2003_CLASSLOADING">
         <code>@techreport{ONJAVA_2003_CLASSLOADING, <br />
 &nbsp;&nbsp;author =	{Andreas Schaefer}, <br />
 &nbsp; title =	{Inside Class Loaders}, <br />
 &nbsp; month =	Nov, year =	{2003}, <br />
 &nbsp; url = {http://www.onjava.com/pub/a/onjava/2003/11/12/classloader.html}<br />
}</code>
    </div>
</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.techjava.de/topics/2008/01/java-class-loading/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

