<?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>javablog.ch&#187; Classloader</title>
	<atom:link href="http://www.javablog.ch/category/classloader/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.javablog.ch</link>
	<description>Keep on learning...</description>
	<lastBuildDate>Sun, 04 Dec 2011 17:15:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Kurze Einführung in ClassLoader &#8211; Teil 4</title>
		<link>http://www.javablog.ch/2006/06/18/kurze-einfuhrung-in-classloader-teil-4/</link>
		<comments>http://www.javablog.ch/2006/06/18/kurze-einfuhrung-in-classloader-teil-4/#comments</comments>
		<pubDate>Sun, 18 Jun 2006 17:10:06 +0000</pubDate>
		<dc:creator>HeikoMaass</dc:creator>
				<category><![CDATA[Classloader]]></category>

		<guid isPermaLink="false">http://www.javablog.ch/?p=16</guid>
		<description><![CDATA[Der Servlet Container Apache Tomcat ist die Referenzimplementierung der Servlet- und JSP-API. In ihm laufen Webapplikationen, die zur Laufzeit installiert (&#8220;deployed&#8221;) und deinstalliert (&#8220;undeployed&#8221;) werden können. Damit das funktioniert, muss Tomcat für jede Webapplikation einen eigenen ClassLoader zur Verfügung stellen. Zudem kann es Klassen geben, die von mehreren Webapplikationen gemeinsam verwendet werden. Tomcat löst diese [...]]]></description>
			<content:encoded><![CDATA[<p>Der Servlet Container <a href="http://tomcat.apache.org">Apache Tomcat</a> ist die Referenzimplementierung der Servlet- und JSP-API. In ihm laufen Webapplikationen, die zur Laufzeit installiert (&#8220;deployed&#8221;) und deinstalliert (&#8220;undeployed&#8221;) werden können. Damit das funktioniert, muss Tomcat für jede Webapplikation einen eigenen ClassLoader zur Verfügung stellen. Zudem kann es Klassen geben, die von mehreren Webapplikationen gemeinsam verwendet werden.</p>
<p>Tomcat löst diese Anforderungen durch folgende ClassLoader Hierarchie: </p>
<p><img src="/wp-content/uploads/2006/06/classloader_teil4.png" alt="ClassLoader Hierarchie des Apache Tomcats." /></p>
<p><strong>Common:</strong><br />
Alle Klassen im <code>/common</code> Verzeichnis des Servers  sind sowohl in den Webapplikationen und für Tomcat selbst sichtbar. Hier werden üblicherweise Datenbank-Treiber abgelegt. Entwickler sollten hier keine eigenen JAR-Dateien platzieren, um eventuelle Kompatibilitätskonflikte mit Tomcat zu vermeiden.</p>
<p><strong>Shared:</strong><br />
Die im /shared Verzeichnis befindlichen Klassen stehen allen Webapplikationen zur Verfügung. Sie müssen diese Klassen dann nicht mehr selbst mitbringen. Ein weitere Anwendungszweck ist die Kommunikation zwischen Webapplikationen oder der Einsatz von Singletons, die von mehreren Webapplikationen benötigt werden. </p>
<p><strong>Server:</strong><br />
Die vom Tomcat Server benötigten Klassen befinden sich im /server Verzeichnis. Die Webapplikationen haben keinen Zugriff darauf. </p>
<p><strong>WebApp:</strong><br />
Jede Webapplikation besitzt einen eigenen ClassLoader, der unterhalb vom Shared ClassLoader liegt. Interessant ist, dass hier der Delegationsmechanismus umgekehrt wird:</p>
<blockquote><p>It is recommended also that the application class loader be implemented so<br />
that classes and resources packaged within the WAR are loaded in preference to<br />
classes and resources residing in container-wide library JARs.</p></blockquote>
<p><a href="http://jcp.org/aboutJava/communityprocess/final/jsr154/index.html">SRV 9.7.2</a></p>
<p>Auf gut deutsch: Der ClassLoader der Webapplikation versucht zuerst die Klasse zu laden und erst dann wird delegiert. Somit können Webapplikationen Klassen vom Shared oder Common ClassLoader &#8220;überschreiben&#8221; (z.B. um zu garantieren, dass genau eine bestimmte Version einer Library zum Einsatz kommt).</p>
<p>Mehr zum Thema ClassLoader im Tomcat gibts direkt auf der <a href="http://tomcat.apache.org/tomcat-5.5-doc/class-loader-howto.html">Tomcat-Homepage</a>. Soweit so gut. Mit diesem Teil ist die ClassLoader &#8211; Reihe vorerst abgeschlossen <img src='http://www.javablog.ch/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>P.S.: Noch ein Blogtipp von mir: Auf  <a href="http://leo.freeflux.net/blog/">http://leo.freeflux.net/blog</a> gibts ebenfalls interessante Javaartikel.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javablog.ch/2006/06/18/kurze-einfuhrung-in-classloader-teil-4/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Kurze Einführung in ClassLoader &#8211; Teil 3</title>
		<link>http://www.javablog.ch/2006/06/11/kurze-einfuhrung-in-classloader-teil-3/</link>
		<comments>http://www.javablog.ch/2006/06/11/kurze-einfuhrung-in-classloader-teil-3/#comments</comments>
		<pubDate>Sun, 11 Jun 2006 20:22:03 +0000</pubDate>
		<dc:creator>HeikoMaass</dc:creator>
				<category><![CDATA[Classloader]]></category>

		<guid isPermaLink="false">http://www.javablog.ch/?p=15</guid>
		<description><![CDATA[Ein eigener ClassLoader muss von java.lang.ClassLoader ableiten und sollte die findClass-Methode überschreiben. Diese Methode benötigt den vollständigen Klassennamen und gibt die geladene Klasse in Form eines Class Objektes zurück. Der nachfolgende ClassLoader MyClassLoader lädt die Klassen aus einem übergebenen Dateipfad. Wie in Java üblich, wird die Packagestruktur auf das Filesystem übertragen (d.h. die Klasse ch.javablog.reloadable.MyTest [...]]]></description>
			<content:encoded><![CDATA[<p>Ein eigener ClassLoader muss von <code>java.lang.ClassLoader</code> ableiten und sollte die <code>findClass</code>-Methode überschreiben. Diese Methode benötigt den vollständigen Klassennamen und gibt die geladene Klasse in Form eines <code>Class </code>Objektes zurück. </p>
<p>Der nachfolgende ClassLoader <code>MyClassLoader</code> lädt die Klassen aus einem übergebenen Dateipfad. Wie in Java üblich, wird die Packagestruktur auf das Filesystem übertragen (d.h. die Klasse <code>ch.javablog.reloadable.MyTest</code> liegt unter <code>../ch/javablog/reloadable</code>). </p>
<p>Im Konstruktor von <code>MyClassLoader</code> wird der &#8220;parent class loader&#8221; übergeben, damit der Delegationsmechanismus funktionieren kann. Erst wenn der &#8220;parent class loader&#8221; die zu ladende Klasse nicht finden konnte, wird unsere <code>findClass</code>-Methode aufgerufen. Dort lesen wir das &#8220;.class&#8221;-File ein und erzeugen ein byte-Feld (Zeile 23-29). </p>
<p>Dieses byte-Feld wird für die Methode <code>defineClass</code> der Oberklasse benötigt, denn sie macht die eigentliche Arbeit und generiert aus dem byte-Feld eine Java Klasse <img src='http://www.javablog.ch/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><strong>ch.javablog.MyClassLoader:</strong></p>
<div class="codeblock">
<div class="java" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #222222; font-weight: bold;">public</span> <span style="color: #222222; font-weight: bold;">class</span> MyClassLoader <span style="color: #222222; font-weight: bold;">extends</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AClassLoader+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #415D77; ">ClassLoader</span></a> <span style="color: #111111;">&#123;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #222222; font-weight: bold;">private</span> <span style="color: #222222; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #415D77; ">String</span></a> startPath;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #222222; font-weight: bold;">private</span> <span style="color: #222222; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #415D77; ">String</span></a> fileSeparator = <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #415D77; ">System</span></a>.<span style="color: #537799;font-weight: bold;">getProperty</span><span style="color: #111111;">&#40;</span><span style="color: #CF6F35;">&quot;file.separator&quot;</span><span style="color: #111111;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #222222; font-weight: bold;">public</span> MyClassLoader<span style="color: #111111;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AClassLoader+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #415D77; ">ClassLoader</span></a> parent, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #415D77; ">String</span></a> startPath<span style="color: #111111;">&#41;</span> <span style="color: #111111;">&#123;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #222222; font-weight: bold;">super</span><span style="color: #111111;">&#40;</span>parent<span style="color: #111111;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #222222; font-weight: bold;">this</span>.<span style="color: #537799;font-weight: bold;">startPath</span> = startPath;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #111111;">&#125;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #222222; font-weight: bold;">protected</span> <span style="color: #222222; font-weight: bold;">Class</span> findClass<span style="color: #111111;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #415D77; ">String</span></a> name<span style="color: #111111;">&#41;</span> <span style="color: #222222; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AClassNotFoundException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #415D77; ">ClassNotFoundException</span></a> <span style="color: #111111;">&#123;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// Absoluten Pfad zur Klasse finden</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AStringBuffer+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #415D77; ">StringBuffer</span></a> path &nbsp; &nbsp; &nbsp; = <span style="color: #222222; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AStringBuffer+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #415D77; ">StringBuffer</span></a><span style="color: #111111;">&#40;</span><span style="color: #222222; font-weight: bold;">this</span>.<span style="color: #537799;font-weight: bold;">startPath</span><span style="color: #111111;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #415D77; ">String</span></a><span style="color: #111111;">&#91;</span><span style="color: #111111;">&#93;</span> splittedName &nbsp; = name.<span style="color: #537799;font-weight: bold;">split</span><span style="color: #111111;">&#40;</span><span style="color: #CF6F35;">&quot;<span style="color: #000099; font-weight: bold;">\\</span><span style="color: #000099; font-weight: bold;">\\</span>.&quot;</span><span style="color: #111111;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AList+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #415D77; ">List</span></a> splittedNameList &nbsp; = <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AArrays+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #415D77; ">Arrays</span></a>.<span style="color: #537799;font-weight: bold;">asList</span><span style="color: #111111;">&#40;</span>splittedName<span style="color: #111111;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AIterator+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #415D77; ">Iterator</span></a> it = splittedNameList.<span style="color: #537799;font-weight: bold;">iterator</span><span style="color: #111111;">&#40;</span><span style="color: #111111;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #222222; font-weight: bold;">while</span> <span style="color: #111111;">&#40;</span>it.<span style="color: #537799;font-weight: bold;">hasNext</span><span style="color: #111111;">&#40;</span><span style="color: #111111;">&#41;</span><span style="color: #111111;">&#41;</span> <span style="color: #111111;">&#123;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #415D77; ">String</span></a> pathFragment = <span style="color: #111111;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #415D77; ">String</span></a><span style="color: #111111;">&#41;</span> it.<span style="color: #537799;font-weight: bold;">next</span><span style="color: #111111;">&#40;</span><span style="color: #111111;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; path.<span style="color: #537799;font-weight: bold;">append</span><span style="color: #111111;">&#40;</span>fileSeparator + pathFragment<span style="color: #111111;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #111111;">&#125;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; path.<span style="color: #537799;font-weight: bold;">append</span><span style="color: #111111;">&#40;</span><span style="color: #CF6F35;">&quot;.class&quot;</span><span style="color: #111111;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #415D77; ">System</span></a>.<span style="color: #537799;font-weight: bold;">out</span>.<span style="color: #537799;font-weight: bold;">println</span><span style="color: #111111;">&#40;</span><span style="color: #CF6F35;">&quot;MyClassLoader - loading: &quot;</span> + path.<span style="color: #537799;font-weight: bold;">toString</span><span style="color: #111111;">&#40;</span><span style="color: #111111;">&#41;</span><span style="color: #111111;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// Klasse aus Datei laden</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AFile+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #415D77; ">File</span></a> classFile = <span style="color: #222222; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AFile+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #415D77; ">File</span></a><span style="color: #111111;">&#40;</span>path.<span style="color: #537799;font-weight: bold;">toString</span><span style="color: #111111;">&#40;</span><span style="color: #111111;">&#41;</span><span style="color: #111111;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #222222; font-weight: bold;">int</span> fileLength = <span style="color: #111111;">&#40;</span><span style="color: #222222; font-weight: bold;">int</span><span style="color: #111111;">&#41;</span> classFile.<span style="color: #537799;font-weight: bold;">length</span><span style="color: #111111;">&#40;</span><span style="color: #111111;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #222222; font-weight: bold;">byte</span><span style="color: #111111;">&#91;</span><span style="color: #111111;">&#93;</span> fileContent = <span style="color: #222222; font-weight: bold;">new</span> <span style="color: #222222; font-weight: bold;">byte</span><span style="color: #111111;">&#91;</span>fileLength<span style="color: #111111;">&#93;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AFileInputStream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #415D77; ">FileInputStream</span></a> fis = <span style="color: #222222; font-weight: bold;">null</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #222222; font-weight: bold;">try</span> <span style="color: #111111;">&#123;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fis = <span style="color: #222222; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AFileInputStream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #415D77; ">FileInputStream</span></a><span style="color: #111111;">&#40;</span>classFile<span style="color: #111111;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fis.<span style="color: #537799;font-weight: bold;">read</span><span style="color: #111111;">&#40;</span>fileContent<span style="color: #111111;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// Erzeugt aus dem byte Feld ein Class Object.</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #222222; font-weight: bold;">return</span> <span style="color: #222222; font-weight: bold;">super</span>.<span style="color: #537799;font-weight: bold;">defineClass</span><span style="color: #111111;">&#40;</span>name, fileContent, <span style="color: #cc66cc;">0</span>, fileLength<span style="color: #111111;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #111111;">&#125;</span> <span style="color: #222222; font-weight: bold;">catch</span> <span style="color: #111111;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #415D77; ">Exception</span></a> e<span style="color: #111111;">&#41;</span> <span style="color: #111111;">&#123;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #222222; font-weight: bold;">throw</span> <span style="color: #222222; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AClassNotFoundException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #415D77; ">ClassNotFoundException</span></a><span style="color: #111111;">&#40;</span>e.<span style="color: #537799;font-weight: bold;">toString</span><span style="color: #111111;">&#40;</span><span style="color: #111111;">&#41;</span><span style="color: #111111;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #111111;">&#125;</span> <span style="color: #222222; font-weight: bold;">finally</span> <span style="color: #111111;">&#123;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #222222; font-weight: bold;">if</span> <span style="color: #111111;">&#40;</span>fis != <span style="color: #222222; font-weight: bold;">null</span><span style="color: #111111;">&#41;</span> <span style="color: #111111;">&#123;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #222222; font-weight: bold;">try</span> <span style="color: #111111;">&#123;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fis.<span style="color: #537799;font-weight: bold;">close</span><span style="color: #111111;">&#40;</span><span style="color: #111111;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #111111;">&#125;</span> <span style="color: #222222; font-weight: bold;">catch</span> <span style="color: #111111;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #415D77; ">Exception</span></a> ignored<span style="color: #111111;">&#41;</span> <span style="color: #111111;">&#123;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #111111;">&#125;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #111111;">&#125;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #111111;">&#125;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #111111;">&#125;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #111111;">&#125;</span></div></li></ol></div>
</div>
<p>In einer kleinen Main-Methode probieren wir <code>MyClassLoader </code>aus. Wichtig: Die Klasse <code>ch.javablog.reloadable.MyTest</code> befindet sich nicht im CLASSPATH, sondern in dem Verzeichnis &#8220;myclasses&#8221; (parallel zum &#8220;classes&#8221; Verzeichnis). </p>
<p><img src="/wp-content/uploads/2006/06/classloader_teil3a.png" alt="classes und myclasses Verzeichnis liegen auf der selben Ebene" /></p>
<p><strong>ch.javablog.Main :</strong></p>
<div class="codeblock">
<div class="java" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #222222; font-weight: bold;">public</span> <span style="color: #222222; font-weight: bold;">static</span> <span style="color: #222222; font-weight: bold;">void</span> main<span style="color: #111111;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #415D77; ">String</span></a><span style="color: #111111;">&#91;</span><span style="color: #111111;">&#93;</span> args<span style="color: #111111;">&#41;</span> <span style="color: #222222; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #415D77; ">Exception</span></a><span style="color: #111111;">&#123;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AClassLoader+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #415D77; ">ClassLoader</span></a> clCurrent = <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AThread+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #415D77; ">Thread</span></a>.<span style="color: #537799;font-weight: bold;">currentThread</span><span style="color: #111111;">&#40;</span><span style="color: #111111;">&#41;</span>.<span style="color: #537799;font-weight: bold;">getContextClassLoader</span><span style="color: #111111;">&#40;</span><span style="color: #111111;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AClassLoader+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #415D77; ">ClassLoader</span></a> clNew = <span style="color: #222222; font-weight: bold;">new</span> MyClassLoader<span style="color: #111111;">&#40;</span>clCurrent, <span style="color: #CF6F35;">&quot;.&quot;</span> + fileSeparator + <span style="color: #CF6F35;">&quot;myclasses&quot;</span><span style="color: #111111;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #222222; font-weight: bold;">Class</span> myTest = clNew.<span style="color: #537799;font-weight: bold;">loadClass</span><span style="color: #111111;">&#40;</span><span style="color: #CF6F35;">&quot;ch.javablog.reloadable.MyTest&quot;</span><span style="color: #111111;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #415D77; ">System</span></a>.<span style="color: #537799;font-weight: bold;">out</span>.<span style="color: #537799;font-weight: bold;">println</span><span style="color: #111111;">&#40;</span>myTest + <span style="color: #CF6F35;">&quot; classloader: &quot;</span> + myTest.<span style="color: #537799;font-weight: bold;">getClassLoader</span><span style="color: #111111;">&#40;</span><span style="color: #111111;">&#41;</span><span style="color: #111111;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #111111;">&#125;</span></div></li></ol></div>
</div>
<p><strong>Ausgabe:</strong><br />
<code>MyClassLoader - loading: .\myclasses\ch\javablog\reloadable\MyTest.class<br />
class ch.javablog.reloadable.MyTest classloader: ch.javablog.MyClassLoader@923e30</code></p>
<p><strong>Interpretation:</strong><br />
Die <code>MyTest</code> wurde erfolgreich von <code>MyClassLoader</code> geladen. Wenn <code>MyTest </code>weitere Klassen importiert, werden sie ebenfalls von <code>MyClassLoader </code>geladen. Die untere Abbildung zeigt die Position unseres ClassLoaders in der ClassLoader-Hierarchie.</p>
<p><img src="/wp-content/uploads/2006/06/classloader_teil3.png" alt="ClassLoader Struktur Teil 3" /></p>
<p>Im nächsten Teil geht es um die ClassLoader im Apache Tomcat, und warum er einen anderen Delegationsmechanismus einsetzt.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javablog.ch/2006/06/11/kurze-einfuhrung-in-classloader-teil-3/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Kurze Einführung in ClassLoader &#8211; Teil 2</title>
		<link>http://www.javablog.ch/2006/06/05/kurze-einfuhrung-in-classloader-teil-2/</link>
		<comments>http://www.javablog.ch/2006/06/05/kurze-einfuhrung-in-classloader-teil-2/#comments</comments>
		<pubDate>Mon, 05 Jun 2006 11:05:04 +0000</pubDate>
		<dc:creator>HeikoMaass</dc:creator>
				<category><![CDATA[Classloader]]></category>

		<guid isPermaLink="false">http://www.javablog.ch/?p=14</guid>
		<description><![CDATA[Wenn ein Class Loader eine Klasse laden muss, delegiert er die Anfrage zunächst an seinen &#8220;parent class loader&#8221;. Erst wenn dieser die gewünschte Klasse nicht finden konnte, probiert er es selber die Klasse zu laden. Das testen wir doch gerade mal. Wir schreiben zwei Klassen: Customer und Main. Beide Klassen befinden sich im CLASSPATH. In [...]]]></description>
			<content:encoded><![CDATA[<p>Wenn ein Class Loader eine Klasse laden muss, delegiert er die Anfrage zunächst an seinen &#8220;parent class loader&#8221;. Erst wenn dieser die gewünschte Klasse nicht finden konnte, probiert er es selber die Klasse zu laden.<br />
Das testen wir doch gerade mal. Wir schreiben zwei Klassen: <code>Customer</code> und <code>Main</code>. Beide Klassen befinden sich im CLASSPATH. In der <code>main</code>-Methode der <code>Main</code>-Klasse werden folgende Objekte geholt:</p>
<ul>
<li><code>ClassLoader </code>des aktuellen Threads</li>
<li><code>ClassLoader </code>von <code>java.lang.String</code></li>
<li><code>ClassLoader </code>von <code>ch.javablog.Main</code></li>
<li><code>ClassLoader </code>von <code>ch.javablog.Customer</code></li>
</ul>
<p>Alle diese Objekte geben wir auf der Kommandozeile aus.</p>
<p><strong>Beispielcode:</strong></p>
<div class="codeblock">
<div class="java" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #222222; font-weight: bold;">public</span> <span style="color: #222222; font-weight: bold;">static</span> <span style="color: #222222; font-weight: bold;">void</span> main<span style="color: #111111;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #415D77; ">String</span></a><span style="color: #111111;">&#91;</span><span style="color: #111111;">&#93;</span> args<span style="color: #111111;">&#41;</span> <span style="color: #111111;">&#123;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AClassLoader+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #415D77; ">ClassLoader</span></a> clCurrent = <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AThread+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #415D77; ">Thread</span></a>.<span style="color: #537799;font-weight: bold;">currentThread</span><span style="color: #111111;">&#40;</span><span style="color: #111111;">&#41;</span>.<span style="color: #537799;font-weight: bold;">getContextClassLoader</span><span style="color: #111111;">&#40;</span><span style="color: #111111;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #415D77; ">System</span></a>.<span style="color: #537799;font-weight: bold;">out</span>.<span style="color: #537799;font-weight: bold;">println</span><span style="color: #111111;">&#40;</span><span style="color: #CF6F35;">&quot;current context classloader: &quot;</span> + clCurrent<span style="color: #111111;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AClassLoader+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #415D77; ">ClassLoader</span></a> clString = <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #415D77; ">String</span></a>.<span style="color: #222222; font-weight: bold;">class</span>.<span style="color: #537799;font-weight: bold;">getClassLoader</span><span style="color: #111111;">&#40;</span><span style="color: #111111;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #415D77; ">System</span></a>.<span style="color: #537799;font-weight: bold;">out</span>.<span style="color: #537799;font-weight: bold;">println</span><span style="color: #111111;">&#40;</span><span style="color: #CF6F35;">&quot;java.lang.String classloader: &quot;</span> + clString<span style="color: #111111;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AClassLoader+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #415D77; ">ClassLoader</span></a> clCustomer = Customer.<span style="color: #222222; font-weight: bold;">class</span>.<span style="color: #537799;font-weight: bold;">getClassLoader</span><span style="color: #111111;">&#40;</span><span style="color: #111111;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #415D77; ">System</span></a>.<span style="color: #537799;font-weight: bold;">out</span>.<span style="color: #537799;font-weight: bold;">println</span><span style="color: #111111;">&#40;</span><span style="color: #CF6F35;">&quot;ch.javablog.Customer classloader: &quot;</span> + clCustomer<span style="color: #111111;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AClassLoader+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #415D77; ">ClassLoader</span></a> clMain = Main.<span style="color: #222222; font-weight: bold;">class</span>.<span style="color: #537799;font-weight: bold;">getClassLoader</span><span style="color: #111111;">&#40;</span><span style="color: #111111;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #415D77; ">System</span></a>.<span style="color: #537799;font-weight: bold;">out</span>.<span style="color: #537799;font-weight: bold;">println</span><span style="color: #111111;">&#40;</span><span style="color: #CF6F35;">&quot;ch.javablog.Main classloader: &quot;</span> + clMain<span style="color: #111111;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #111111;">&#125;</span></div></li></ol></div>
</div>
<p><strong>Ausgabe auf der Kommandozeile:</strong><br />
<code>current context classloader: sun.misc.Launcher$AppClassLoader@133056f<br />
java.lang.String classloader: null<br />
ch.javablog.Customer classloader: sun.misc.Launcher$AppClassLoader@133056f<br />
ch.javablog.Main classloader: sun.misc.Launcher$AppClassLoader@133056f<br />
</code></p>
<p><strong>Interpretation:</strong><br />
Der <code>ClassLoader </code>des Threads ist der System ClassLoader (sun.misc.Launcher). Er lädt die <code>String</code>-Klasse nicht selber, sondern delegiert das Laden der <code>String</code>-Klasse an seinen &#8220;parent class loader&#8221;, dem Bootstrap ClassLoader. Dieser wird nicht als <code>ClassLoader</code>-Objekt repräsentiert, stattdessen wird beim Zugriffsversuch <code>null </code>zurückgegeben. </p>
<p>Die Klassen <code>Customer</code> und <code>Main</code> werden vom System ClassLoader auch an den Bootstrap ClassLoader delegiert. Der kennt sie aber nicht und von daher muss der System ClassLoader diese Klassen selber laden.</p>
<p><img src="/wp-content/uploads/2006/06/classloader_teil2.png" alt="ClassLoader Struktur" /></p>
<p>Bis jetzt alles noch sehr einfach. Im nächsten Teil schreiben wir einen eigenen ClassLoader&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javablog.ch/2006/06/05/kurze-einfuhrung-in-classloader-teil-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Kurze Einführung in ClassLoader &#8211; Teil 1</title>
		<link>http://www.javablog.ch/2006/06/04/kurze-einfuhrung-in-classloader/</link>
		<comments>http://www.javablog.ch/2006/06/04/kurze-einfuhrung-in-classloader/#comments</comments>
		<pubDate>Sun, 04 Jun 2006 14:30:47 +0000</pubDate>
		<dc:creator>HeikoMaass</dc:creator>
				<category><![CDATA[Classloader]]></category>

		<guid isPermaLink="false">http://www.javablog.ch/?p=13</guid>
		<description><![CDATA[Jeder kennt Sie, jeder hasst sie: ClassCastException, ClassNotFoundException und NoClassDefFoundError. Das Laden von Klassen durch ClassLoader erlaubt zwar wunderbare Dinge wie &#8220;Hot Deployment&#8221; (Dynamisches Austauschen von Klassen zur Laufzeit), sorgt aber auch für eine Reihe von potentiellen Stolpersteinen. Leider stolpert man gerade dann drüber, wenn man überhaupt keine Zeit für Ursachenforschung hat . Dewegen gibts [...]]]></description>
			<content:encoded><![CDATA[<p>Jeder kennt Sie, jeder hasst sie: <code>ClassCastException</code>, <code>ClassNotFoundException</code> und <code>NoClassDefFoundError</code>. </p>
<p>Das Laden von Klassen durch ClassLoader erlaubt zwar wunderbare Dinge wie &#8220;Hot Deployment&#8221; (Dynamisches Austauschen von Klassen zur Laufzeit), sorgt aber auch für eine Reihe von potentiellen Stolpersteinen. Leider stolpert man gerade dann drüber, wenn man überhaupt keine Zeit für Ursachenforschung hat <img src='http://www.javablog.ch/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> . Dewegen gibts hier eine kleine ClassLoader-Einführung.</p>
<p>Eins vorweg: Es gibt zwei Typen von ClassLoader:</p>
<blockquote><p>There are two types of class loaders: user-defined class loaders and the bootstrap class loader supplied by the Java virtual machine. Every user-defined class loader is an instance of a subclass of the abstract class ClassLoader.</p></blockquote>
<p>  <a href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/ConstantPool.doc.html#72007">JLS (5.3)</a></p>
<p>ClassLoader sind hierarchisch aufgebaut. D.h. jeder besitzt genau einen &#8220;parent class loader&#8221;. Bis auf den &#8220;bootstrap class loader&#8221;. Denn er steht an der obersten Stelle der ClassLoader-Hierarchie und ist für das Laden der Java API (z.B. <code>String </code>oder <code>Integer</code>) zust�ndig (Konkret lädt er die Klassen aus <code>rt.jar</code>). Unter ihm liegt der erste &#8220;user-defined class loader&#8221;: Der System ClassLoader. Er lädt die Klassen, die im CLASSPATH angegeben werden. </p>
<p>Das nachfolgende Bild zeigt die ClassLoader-Struktur im einfachsten Fall (keine Sorge, es wird noch komplexer&#8230;)</p>
<p><img src="/wp-content/uploads/2006/06/classloader_teil1.png" alt="" /></p>
<p>So viel für heute. Im nächsten Teil gehen wir auf den Delegation Mechanismus ein&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javablog.ch/2006/06/04/kurze-einfuhrung-in-classloader/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

