class loader basic
TRANSCRIPT
Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
ClassLoader 개요
강 명 철
2006.11
Core Java
Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
목차
I. Class LoaderII. Custom Class LoaderIII. Diagnosing
국민은행 IT 관리자특강 : – July 30, 2004Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
-3-
I. ClassLoader 1. JVM Architecture
국민은행 IT 관리자특강 : – July 30, 2004Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
-4-
I. ClassLoader
Class loading takes place in two phases : Loading Linking or resolution
2. How Classes are Loaded ?
국민은행 IT 관리자특강 : – July 30, 2004Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
-5-
3. Class Class in JavaI. ClassLoader
Instances of class java.lang.Class represent classes and interfaces in a running Java application
Class has no public constructors Any applications cannot instantiate it Class is automatically instantiated by JVM/class loader, when a class loading process is done
Each instance of Class keeps a reference to a class loader that created it
국민은행 IT 관리자특강 : – July 30, 2004Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
-6-
4. Class LoadingI. ClassLoader
When you define a class, say Foo, and initiate it ( to create its instance) i.e. when you execute new Foo();
A local JVM (class loader) tries to Obtain the class definition of Foo
Given the name of a class (i.e. Foo), the class loader locates a binary representation of Foo’s class definition Typically, it transforms the class name into a file name, and reads a class file of that name (e.g. Foo.class)
from the local file system If this step fails, JVM return a ClassDefNotFound exception
Parse the obtained class definition into internal structures in the method area of the JVM The method area is a logical area of memory in the JVM that is used to store information about loaded class
es/interfaces Create an instance of Class, which is associated with Foo
These 3 activities are collectively called class loading. After successful class loading, an instance of Foo is created and returned
국민은행 IT 관리자특강 : – July 30, 2004Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
-7-
5. Class Loaders in JVMI. ClassLoader
Every JVM has two types of class loaders Default class loaders
Bootstrap class loader Used to find and load a set of standard class libraries (i.e. java.* classes) that are located in $JAVA_HOME
$/jre/lib -> e.g. $JAVA_HOME$/jre/lib/rt.jar
Extension class loader Used to find and load a set of extension class libraries (i.e. javax.* classes) that are located in $JAVA_HOM
E$/jre/lib/ext System class loader (java.lang.Classloader)
Used to find and load user-defined classes
Custom class loaders If a user wants to change the behavior of system class loader, the user can extend java.lang.Classloader as a Ja
va class. The class is called a custom class loader A custom class loader overloads the methods defined in java.lang.Classloader to implement its own class loadin
g behavior. Different custom class loaders can specialize different class loading behaviors (policies)
국민은행 IT 관리자특강 : – July 30, 2004Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
-8-
6. Class Loaders RulesI. ClassLoader
Consistency Rule Class loaders never load the same class more than once
Delegation Rule Class loaders always consult a parent class loader before loading a class
Visibility Rule Classes can only “see” other classes loaded by their class loader’s delegation, the recursive
set of a class’s loader and all its parent loaders
국민은행 IT 관리자특강 : – July 30, 2004Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
-9-
6. Visibility I. ClassLoader
국민은행 IT 관리자특강 : – July 30, 2004Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
-10-
6. Visibility 실습I. ClassLoader
Java Statics
public class Dummy { public static int staticCount = 0; public Dummy(){ staticCount++; System.out.println("Instance #" + staticCount + " constructed."); }}
public class StaticTest {
public static void main(String[] args) {new Dummy();new Dummy();new Dummy();new Dummy();new Dummy();
}}
Instance #1 constructed.Instance #2 constructed.Instance #3 constructed.Instance #4 constructed.Instance #5 constructed.
국민은행 IT 관리자특강 : – July 30, 2004Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
-11-
6. Visibility 실습I. ClassLoader
Java Statics
public class Dummy { public static int staticCount = 0; public Dummy(){ staticCount++; System.out.println("Instance #" + staticCount + " constructed."); }}
public class StaticTestClassLoader {
public static void main(String[] args) throws Exception { URL[] url = {new File("../target/bin").toURL()}; URLClassLoader cl1 = new URLClassLoader(url); URLClassLoader cl2 = new URLClassLoader(url); URLClassLoader cl3 = new URLClassLoader(url); cl1.loadClass("Dummy").newInstance(); cl2.loadClass("Dummy").newInstance(); cl3.loadClass("Dummy").newInstance(); }}
Instance #1 constructed.Instance #1 constructed.Instance #1 constructed.
국민은행 IT 관리자특강 : – July 30, 2004Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
-12-
7. Delegation ModelI. ClassLoader
class NetworkClassLoader extends ClassLoader { String host; int port;
public Class findClass(String name) { byte[] b = loadClassData(name); return defineClass(name, b, 0, b.length); }
private byte[] loadClassData(String name) { // load the class data from the connection . . . } }
국민은행 IT 관리자특강 : – July 30, 2004Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
-13-
7. Delegation ModelI. ClassLoader
국민은행 IT 관리자특강 : – July 30, 2004Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
-14-
7. Delegation Model 실습I. ClassLoader
Java 1.1 Style
public class TestClassLoader extends ClassLoader { protected Hashtable m_classes = new Hashtable(); protected boolean m_cacle = false;
protected byte[] loadClassBytes(String name) throws ClassNotFoundException{ byte[] result = null; try{ FileInputStream in = new FileInputStream("../Target/bin/" + name.replace('.', '/') + ".class"); ByteArrayOutputStream data = new ByteArrayOutputStream();
int ch; while((ch = in.read()) != -1) data.write(ch); result = data.toByteArray(); } catch (Exception e){ throw new ClassNotFoundException(name); } return result; }
protected synchronized Class loadClass (String name, boolean resolve) throws ClassNotFoundException{ System.out.println(" 요청된 Class : " + name); Class result = (Class)m_classes.get(name); try{ if(result==null) return super.findSystemClass(name); else return result; } catch(Exception e) {} System.out.println("TestClassLoader Loading Class : " + name); byte bytes[] = loadClassBytes(name); result = defineClass(name, bytes, 0, bytes.length); if(result == null) throw new ClassNotFoundException(name); if(resolve) resolveClass(result); m_classes.put(name, result); return result; }}
국민은행 IT 관리자특강 : – July 30, 2004Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
-15-
7. Delegation Model 실습I. ClassLoader
Java 1.2 Style
public class TestClassLoader extends ClassLoader {
protected byte[] loadClassBytes(String name) throws ClassNotFoundException{ byte[] result = null; try{ FileInputStream in = new FileInputStream("../Target/bin/" + name.replace('.', '/') + ".class"); ByteArrayOutputStream data = new ByteArrayOutputStream();
int ch; while((ch = in.read()) != -1) data.write(ch); result = data.toByteArray(); } catch (Exception e){ throw new ClassNotFoundException(name); } return result; }
protected Class findClass(String name) throws ClassNotFoundException{ System.out.println(" 요청된 Class : " + name); System.out.println("TestClassLoader Loading Class : " + name);
byte bytes[] = loadClassBytes(name); return defineClass(name, bytes, 0, bytes.length); }}
국민은행 IT 관리자특강 : – July 30, 2004Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
-16-
//java.lanag.ClassLoader 일부 protected synchronized Class loadClass(String name, boolean resolve)
throws ClassNotFoundException {
// First, check if the class has already been loadedClass c = findLoadedClass(name);if (c == null) { try {
if (parent != null) { c = parent.loadClass(name, false);} else { c = findBootstrapClass0(name);}
} catch (ClassNotFoundException e) { // If still not found, then invoke findClass in order // to find the class. c = findClass(name); }}if (resolve) { resolveClass(c);}return c;
}
7. Delegation Model 실습I. ClassLoader
Java 1.2 Style
국민은행 IT 관리자특강 : – July 30, 2004Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
-17-
8. Standard Class LoaderI. ClassLoader
Standard ClassLoader
국민은행 IT 관리자특강 : – July 30, 2004Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
-18-
8. Standard Class LoaderI. ClassLoader
Java Options
국민은행 IT 관리자특강 : – July 30, 2004Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
-19-
국민은행 IT 관리자특강 : – July 30, 2004Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
-20-
public class ClassLoaderTree { public static void printClassLoaderTree(ClassLoader l) { if(l== null){ System.out.println("BootStrap ClassLoader"); return; } ClassLoader p = l.getParent(); if ( p!= null) { printClassLoaderTree(p); } else { System.out.println("BootStrap ClassLoader"); } String u = ""; if ( l instanceof URLClassLoader) { u = getURLs( ((URLClassLoader)l).getURLs()); } // end of if () System.out.println("\t|\n"+l+" " +u);}
8. Standard Class LoaderI. ClassLoader
ClassLoader Tree
public static String getURLs(URL[] urls) { if ( urls == null) { return "{}"; } // end of if () StringBuffer b = new StringBuffer("{"); for ( int i = 0;i<urls.length;i++) { b.append( urls[i] ).append(":"); } // end of for () b.append("}"); return b.toString(); }
public static void main(String[] args) { ClassLoaderTree.printClassLoaderTree (ClassLoaderTree.class.getClassLoader()); }}
국민은행 IT 관리자특강 : – July 30, 2004Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
-21-
java -classpath ./bin ClassLoaderTree
java -Djava.ext.dirs=./bin ClassLoaderTree
java -Xbootclasspath:.\bin;D:\JDK\SDK13\jre\lib\rt.jar;... ClassLoaderTree
8. Standard Class LoaderI. ClassLoader
국민은행 IT 관리자특강 : – July 30, 2004Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
-22-
SummaryI. ClassLoader
ClassLoader Basics
1. What is a class loader?1. mechanism for loading classes into the JVM
2. subclass of java.lang.ClassLoader
2. Hierarchical structure1. Parent-child relationships
3. Uses delegation1. Load requests delegated to parent
ClassLoader-A
Class-X
ClassLoader-B ClassLoader-C
Class-Y Class-Z Class-Z
국민은행 IT 관리자특강 : – July 30, 2004Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
-23-
1. Unidirectional visibility1. Parent-loaded classes cannot see child-
loaded classes
2. Thread.getContextClassloader()
2. Unique Namespaces1. Classes referenced as
classloader.classname
3. Variety of class file locations1. File system
2. Databases
3. Remote server locations
Database
Remote Server
File system
ClassLoader-A
Class-X
ClassLoader-B ClassLoader-CClass-Y Class-Z Class-R Class-D
SummaryI. ClassLoader
ClassLoader Basics
국민은행 IT 관리자특강 : – July 30, 2004Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
-24-
1. Bootstrap ClassLoader1. aka Primordial ClassLoader
2. Created by the JVM
3. Loads the JDK classes included in the JVM
(including java.lang.ClassLoader)
Bootstrap ClassLoader
Extensions ClassLoader
Application
Shared Lib
System ClassLoader
3. System ClassLoader1. loads classes specified in the classpath2. Loads applications, shared libraries, jars, etc.
2. Extensions ClassLoader1. Loads JRE classes
JAR
SummaryI. ClassLoader
ClassLoader Basics
국민은행 IT 관리자특강 : – July 30, 2004Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
-25-
1. Determine Initial ClassLoader
2. Delegate to Parent ClassLoader
3. Class Loaded?
4. No – Delegate to Parent
5. Lastly – If class is not loaded in parent hierarchy then the Initial loader will attempt to load the class itself.
Class Loader A
MyClass
Class Loader B
Class Loader C
System ClassLoader
Bootstrap ClassLoader
Extension ClassLoader
SummaryI. ClassLoader
Typical Loading Scenario
국민은행 IT 관리자특강 : – July 30, 2004Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
-26-Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
목차
I. Class LoaderII. Custom Class
LoaderIII. Diagnosing
국민은행 IT 관리자특강 : – July 30, 2004Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
-27-
1. Java 내부의 ClassLoaderI. Custom ClassLoader
java.security.SecureClassLoader Java 2 Platform 보안부분 지원
Java.net.URLClassLoader Java 2 Runtime Library 내부에서 유일하게 실제 구현된 ClassLoader
sun.applet.AppletClassLoader java.rmi.server.RMIClassLoader
RMI runtime system 에서 class 를 마샬링하고 로딩하는 기능을 하는 wrapper class sun.rmi.server.LoaderHandler Class 에 대한 다리 역할
BootStrap ClassLoader Native Code 로 구현 Sun.boot.class.path 프로퍼티 참조
sun.misc.Launcher$ExtClassLoader Java.ext.dirs 프로퍼티 참조
sun.misc.Launcher$AppClassLoader
국민은행 IT 관리자특강 : – July 30, 2004Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
-28-
public static void main(String[] args) throws Exception { URL[] urlArray = { //new java.io.File("subdir.jar").toURL() // For loading-from-subdir.jar, uncomment the // above line and comment out the below line
new java.io.File("subdir/").toURL() // For loading-from-subdirectory-subdir, // comment out the first line and uncomment // out this one }; URLClassLoader ucl = new URLClassLoader(urlArray); Object obj = ucl.loadClass("Hello").newInstance(); // Hello should print "Hello" to the System.out stream }
1.URLClassLoader 사용 예I. ClassLoader
FileURLClient
국민은행 IT 관리자특강 : – July 30, 2004Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
-29-
public static void main(String[] args) throws Exception { URL[] urlArray = { new URL("http", "www.javageeks.com", "/SSJ/examples/") }; URLClassLoader ucl = new URLClassLoader(urlArray); Object obj = ucl.loadClass("com.javageeks.util.Hello").newInstance(); // Hello should print "Hello from JavaGeeks.com!" to the // System.out stream }
1.URLClassLoader 사용 예I. ClassLoader
HttpURLClient
국민은행 IT 관리자특강 : – July 30, 2004Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
-30-
public static void main(String[] args) throws Exception { URL[] urlArray = { new URL("ftp", "reader:[email protected]", "/examples") };
URLClassLoader ucl = new URLClassLoader(urlArray); Object obj = ucl.loadClass("Hello").newInstance(); }
1.URLClassLoader 사용 예I. ClassLoader
FTPURLClient
국민은행 IT 관리자특강 : – July 30, 2004Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
-31-
2. Why write a ClassLoader ?I. Custom ClassLoader
The default ClassLoader only knows how to load class files from the local file system. 대부분의 경우에 만족
일반적으로 Local File System, Network 으로 부터 File 을 Loading 하는 것 이외에 다음과 같은 용도로 사용됨 Automatically verify a digital signature before executing untrusted code Transparently decrypt code with a user-defined password Create dynamically built classes customized to the user’s specific needs
국민은행 IT 관리자특강 : – July 30, 2004Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
-32-
3. Class Loader 확장I. Custom ClassLoader
findClass(…) Class 이름으로 부터 Byte Code 를 얻는데 사용 defineClass(…) 를 호출하여 Class Object 로 변환함
findResource(…) 주어진 임의의 이름에 대한 Byte Code 를 얻는데 사용 getResource(..) 에서 호출함
findLibrary(…) ClassLoader 가 native library 를 포함한 Class 를 Load 할 때 호출 Binary Data 가 아닌 절대 경로명 만 반환
protected Class findClass(String name) throws ClassNotFoundException { throw new ClassNotFoundException(name); }
protected URL findResource(String name) {return null;
}
protected String findLibrary(String libname) { return null; }
국민은행 IT 관리자특강 : – July 30, 2004Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
-33-
4. Class Loader 실습I. Custom ClassLoader
FileSystemClassLoader Local File System 에서 Class Byte Code 를 뽑아내는 ClassLoader 제작 .jar/.zip 에 대한 부분은 고려하지 않음 ClassLoader Class 를 extends 하여 구현 findClass(String name) 부분 overriding 구현
java -classpath ./bin -Dloader.dir=./bin/ FileSystemClassLoader
국민은행 IT 관리자특강 : – July 30, 2004Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
-34-
4. Class Loader 실습I. Custom ClassLoader
CompilingClassLoader When a class is requested, see if it exists on the disk, in the current directory, or in the appropriate subdire
ctory If the class is not available, but the source is, call the java compiler to generate the class file If the class file does exist, check to see if it is older than its source code. If it is older than the source, call t
he java compiler to generate the class file. If the compile fails, or if for any reason the class file could not be generated from existing source, throw a Cl
assNotFoundException If we still don’t have the class, maybe it’s in some other library, so call findSystemClass to see if that will w
ork If we still don’t have the class, throw a ClassNotFoundException Otherwise, return the class
java -classpath ./bin CCLRun Foo aaa bbb
국민은행 IT 관리자특강 : – July 30, 2004Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
-35-
4. Class Loader 실습I. Custom ClassLoader
HotDeploy
java -classpath ./bin athena.loader.PointClient
국민은행 IT 관리자특강 : – July 30, 2004Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
-36-
4. Class Loader 실습I. Custom ClassLoader
Context Class Loader
java -classpath ./bin DynamicLoader Echo aaa aaa aaa
java -Djava.ext.dirs=./jar -classpath ./bin DynamicLoader Echo aaa aaa aaa
java -Djava.ext.dirs=./jar -classpath ./bin FixedDynamicLoader Echo aaa aaa aaa
국민은행 IT 관리자특강 : – July 30, 2004Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
-37-Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
목차
I. Class LoaderII. Custom Class LoaderIII. Diagnosing
국민은행 IT 관리자특강 : – July 30, 2004Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
-38-
1. Class Not FoundI. Diagnosing
ClassNotFoundException Used String representation
(e.g. ClassLoader.LoadClass(className)) Typically, dependent Class is only visible to children
NoClassDefFoundError Class existed at compile time Typically, dependent Class is only visible to children
NoClassDefFoundException An interesting phenomenon: this Exception is not in the JDK,
but it appears in dozens of topics in developer forums
EJBJAR
STRUTS.JAR
WAR B
AppBForm
WAR A
AppAForm
국민은행 IT 관리자특강 : – July 30, 2004Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
-39-
ClassCastException same Class was loaded by two different ClassLoaders
ClassCircularityError A Class and one of its dependents are both dependent on a third Class; different
ClassLoaders are used to load that third Class
EJBJAR
public void utilityMethod (Object o) { ((Apple)o).doSomething();}
WAR B
Apple
WAR A
Apple
AppleCLASSCASTEXCEPTION
2. Class CastI. Diagnosing
국민은행 IT 관리자특강 : – July 30, 2004Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
-40-
Place diagnostic statements in the application code
Place diagnostic statements in a custom ClassLoader
Place diagnostic statements in the core Java ClassLoaders. The -Xbootclasspath option makes to possible to supersede core Classes in a development environment.
Use the –verbose option
C:\>java -Xbootclasspath:C:\debug;j2sdk1.4.2_03\jre\lib\rt.jar
…[all other boot.class.path jars] app.java
3. Getting Diagnostic Info.I. Diagnosing
국민은행 IT 관리자특강 : – July 30, 2004Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
-41-
import java.io.File;
import java.net.MalformedURLException;
import java.net.URLClassLoader;
import java.net.URL;
import demo.Rabbit;
/**
* Instantiates and displays a Rabbit.
*/
public class TopHat {
public static void main (String args[]) {
try {
// Create a ClassLoader that knows where to find demo. Rabbit
URL rabbitURL = new File("rabbit.jar").toURL();
URL[] urls = new URL[]{rabbitURL};
URLClassLoader rabbitClassLoader = new URLClassLoader(urls,Thread.currentThread().getContextClassLoader());
// Set the ContextClassLoader for the current Thread so that it can find Rabbit.class
Thread.currentThread().setContextClassLoader(rabbitClassLoader);
// Make a Rabbit appear.
System.out.println(new Rabbit());
} catch (MalformedURLException malformedURLException) {
malformedURLException.printStackTrace();
}
}
}
TopHat.java
3. 실습I. Diagnosing
국민은행 IT 관리자특강 : – July 30, 2004Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
-42-
package demo;
public class Rabbit {}
Rabbit.java
rabbit.jarC:\> jar tf rabbit.jardemo/demo.Rabbit.class
C:\> javac –classpath rabbit.jar TopHat.java
C:\> java –classpath ./bin TopHat
Command Line
Compilation
Rabbit.java
3. 실습I. Diagnosing
국민은행 IT 관리자특강 : – July 30, 2004Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
-43-
1. TopHat should instantiate and display a String
2. representation of a Rabbit, even though
3. rabbit.jar is not referenced on the application
4. classpath.
Actual BehaviorC:\>java TopHat
Exception in thread "main" java.lang.NoClassDefFoundError: demo/Rabbit
at TopHat.main(TopHat.java:23)
Desired Behavior
Desired Behavior Vs. Actual Behavior
3. 실습I. Diagnosing
국민은행 IT 관리자특강 : – July 30, 2004Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
-44-
// Create a ClassLoader that knows where to find demo. RabbitURL rabbitURL = new File("rabbit.jar").toURL();URL[] urls = new URL[]{rabbitURL};URLClassLoader rabbitClassLoader = new URLClassLoader(urls,Thread.currentThread().getContextClassLoader());System.out.println("---> ClassLoader for TopHat: " +TopHat.class.getClassLoader()); System.out.println("---> Before setting a custom ContextClassLoader,ContextClassLoader is: “);System.out.println( Thread.currentThread().getContextClassLoader());// Set the ContextClassLoader for the current Thread so that it can find// RabbitThread.currentThread().setContextClassLoader(rabbitClassLoader);System.out.println("---> After setting a custom ContextClassLoaderContextClassLoader is: ");System.out.println( Thread.currentThread().getContextClassLoader()); // Make a Rabbit appear.System.out.println(new Rabbit());
TopHat.java with Diagnostic Statements
3. 실습I. Diagnosing
국민은행 IT 관리자특강 : – July 30, 2004Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
-45-
C:\>java TopHat---> ClassLoader for TopHat:
sun.misc.Launcher$AppClassLoader@e80a59---> Before setting a custom ContextClassLoader,
ContextClassLoader is:sun.misc.Launcher$AppClassLoader@e80a59---> After setting a custom ContextClassLoader,
ContextClassLoader is:java.net.URLClassLoader@eee36cException in thread "main"
java.lang.NoClassDefFoundError: demo/Rabbit at TopHat.main(TopHat.java:28)
Output From Diagnostics in the Application
3. 실습I. Diagnosing
국민은행 IT 관리자특강 : – July 30, 2004Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
-46-
protected Class findClass(final String name) throws ClassNotFoundException { try { return (Class)
AccessController.doPrivileged(new PrivilegedExceptionAction() { public Object run() throws ClassNotFoundException {
String path = name.replace('.', '/').concat(".class"); Resource res = ucp.getResource(path, false); if (res != null) {
try { return defineClass(name, res);
} catch (IOException e) { throw new ClassNotFoundException(name, e);
} } else {
System.out.println(“-- Looked in:”); URL[] urls = getURLs(); for (int i = 0;i<urls.length;i++) { System.out.println(urls[i]); } throw new ClassNotFoundException(name);
} }}, acc);
} catch (java.security.PrivilegedActionException pae) { throw (ClassNotFoundException) pae.getException();}
}
Diagnostics in java.net.URLClassLoader
3. 실습I. Diagnosing
국민은행 IT 관리자특강 : – July 30, 2004Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
-47-
protected synchronized Class loadClass(String name, boolean resolve)throws ClassNotFoundException {Class c = findLoadedClass(name);if (c == null) { try { if (name.equals("demo.Rabbit")) System.out.println("---> " + this + "is asking parent (" + parent + ") to try to load " +
name + "."); if (parent != null) {
c = parent.loadClass(name, false);} else { c = findBootstrapClass0(name);}
} catch (ClassNotFoundException e) { // If still not found, then call findClass in order // to find the class. if (name.equals("demo.Rabbit")) System.out.println("---> " + name + " was not found by parent (" + parent + "), so " + this +
" will try."); c = findClass(name); }}if (resolve) { resolveClass(c);}return c;
}
void addClass(Class c) { if (c.toString().equals("class demo.Rabbit"))
System.out.println("---> " + this + "loaded " + c);
classes.addElement(c); }
private synchronized Class loadClassInternal(String name)throws ClassNotFoundException {
if (name.equals("demo.Rabbit")) { System.out.println("JVM is requesting that " + this +" load "+ name); } return loadClass(name); }
Diagnostics in java.lang.ClassLoader
3. 실습I. Diagnosing
국민은행 IT 관리자특강 : – July 30, 2004Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
-48-
---> JVM is requesting that sun.misc.Launcher$AppClassLoader@e80a59 load demo.Rabbit
---> sun.misc.Launcher$AppClassLoader@e80a59 is asking parent (sun.misc.Launcher$
ExtClassLoader@1ff5ea7) to try to load demo.Rabbit.
---> sun.misc.Launcher$ExtClassLoader@1ff5ea7 is asking parent (null) to try to load demo.Rabbit.
---> demo.Rabbit was not found by parent (null), so sun.misc.Launcher$ExtClassLoader@1ff5ea7 will try.
--> Looked in:
file:/C:/j2sdk1.4.2_03/jre/lib/ext/dnsns.jar
file:/C:/j2sdk1.4.2_03/jre/lib/ext/ldapsec.jar
file:/C:/j2sdk1.4.2_03/jre/lib/ext/localedata.jar
file:/C:/j2sdk1.4.2_03/jre/lib/ext/sunjce_provider.jar
---> demo.Rabbit was not found by parent (sun.misc.Launcher$ExtClassLoader@1ff5ea7), so sun.misc.Launcher$AppClassLoader@e80a59 will try.
--> Looked in:
file:/C:/
Exception in thread "main" java.lang.NoClassDefFoundError: demo/Rabbit
at TopHat.main(TopHat.java:28)
Output from Diagnostics in the Core Java Classes
3. 실습I. Diagnosing
java -Xbootclasspath/p:D:\Classloader\Util\bin -classpath ./bin TopHat
국민은행 IT 관리자특강 : – July 30, 2004Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
-49-
• Regardless of how the Context ClassLoader is set, the classloader that loaded the calling Class will always be used to load a requested Class when no ClassLoader is specified– new ClassName()– Class.forName(“package.ClassName”)
• Context ClassLoaders are used by– JNDI– JAXP
Analysis
3. 실습I. Diagnosing
국민은행 IT 관리자특강 : – July 30, 2004Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
-50-
ClassLoader loader =
Thread.currentThread().getContextClassLoader();
loader.loadClass(“demo.Rabbit”).newInstance();
ClassLoader loader =
Thread.currentThread().getContextClassLoader();
Class.forName(“demo.Rabbit”,true,
loader).newInstance();
– or –
Possible Solutions
3. 실습I. Diagnosing
국민은행 IT 관리자특강 : – July 30, 2004Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
-51-
---> java.net.URLClassLoader@defa1a is asking parent (sun.misc.Launcher$AppClassL
oader@e80a59) to try to load demo.Rabbit.
---> sun.misc.Launcher$AppClassLoader@e80a59 is asking parent (sun.misc.Launcher$
ExtClassLoader@1ff5ea7) to try to load demo.Rabbit.
---> sun.misc.Launcher$ExtClassLoader@1ff5ea7 is asking parent (null) to try to
load demo.Rabbit.
---> demo.Rabbit was not found by parent (null), so sun.misc.Launcher$ExtClassLo
ader@1ff5ea7 will try.
--> Looked in:
file:/C:/j2sdk1.4.2_03/jre/lib/ext/dnsns.jar
file:/C:/j2sdk1.4.2_03/jre/lib/ext/ldapsec.jar
file:/C:/j2sdk1.4.2_03/jre/lib/ext/localedata.jar
file:/C:/j2sdk1.4.2_03/jre/lib/ext/sunjce_provider.jar
---> demo.Rabbit was not found by parent (sun.misc.Launcher$ExtClassLoader@1ff5e
a7), so sun.misc.Launcher$AppClassLoader@e80a59 will try.
--> Looked in:
file:/C:/
---> demo.Rabbit was not found by parent (sun.misc.Launcher$AppClassLoader@e80a5
9), so java.net.URLClassLoader@defa1a will try.
---> java.net.URLClassLoader@defa1a loaded class demo.Rabbit
demo.Rabbit@a18aa2
Rabbit Appears
3. 실습I. Diagnosing
감사합니다 .
Copyright © 2004 Samsung SDS Co.,Ltd. All rights reserved | Confidential
대표전화 : +82-2-6484-0930E-mail: [email protected]://www.sds.samsung.co.kr