You are on page 1of 45

Plug and Play

( 24
) bug

CC++

Windows
(Dynamic Linking Library) Unix
(Share Object)
( Windows
LoadLibrary() GetProcAddress() Win32 API
)

Java
Java Java
Java

Java

Java
Java

?
Java Java
Java
(, .class) public static void
main(String args[])Java

Java Java
.dll .so .class
Java
Java
Java .class

JSP/Servlet Web Server( Web


Container) Application Server EJB Container
Hot Deployment
Web Server servlet
servlet HTTP request servlet
servlet EJB
Container Hot Deployment Application
Server EJB() EJB
Hot Deployment
:
Java Web Server EJB
Container Java Java
Java Hot Deployment
? ! Java

Hot Deployment ?

-
Java ;
Java

2
Java MainA B
:
:A.java
public class A
{
public void print()
{
System.out.println("Using Class A") ;
}
}

:B.java
public class B
{
public void print()
{
System.out.println("Using Class B") ;
}
}

:Main.java
public class Main
{
public static void main(String args[])
{
A a1 = new A() ;
a1.print() ;
B b1 = new B() ;
b1.print() ;
}
}

3
:
java verbose:class Main
:

(class loader)
Java ( Core
Classes)(<JRE >\lib\rt.jar )
Main.classA.class B.class
(.class )

java.exe
Java Runtime Environment(JRE)(.class)
JRE java.exe JRE

4
JRE
\lib\rt.jar java.exe
c:\j2sdk1.4.0\jre java.exe
JRE:

java.exe C:\Program Files\Java\j2re1.4.0 JRE


C:\Program
Files\Java\j2re1.4.0\lib\rt.jar

- (A.class
B.class)(Main.class
A.class B.class )( rt.jar )
A.class
Using Class A B.class Using Class
B
(pre-loading)
Java
I/O (
) Java
(load-on-demand)
Java

:
?

5
Main.java:
:Main.java
public class Main
{
public static void main(String args[])
{
A a1 = new A() ;

B b1 ;

}
}

java verbose:class Main


:

(: B )
(new XXX())


Java

Java
Java

:
:Office.java
public class Office
{
public static void main(String args[])
{
if(args[0].equals("Word"))

6
{
Word w = new Word() ;
w.print() ;
}else if(args[0].equals("Excel"))
{
Excel e = new Excel() ;
e.print() ;
}
}
}

:Word.java
public class Word
{
public void print()
{
System.out.println("Using Word") ;
}
}

:Excel.java
public class Excel
{
public void print()
{
System.out.println("Using Excel") ;
}
}
Office Office Word
Excel
Office.java
:
java Office Word
Word.class:

7
:
java Office Excel
Excel.class:

Java

Java
(
)()

Java
Java
?

Java Java
(implicit)(explicit)

Win32

8
(Dynamic Linking Library)(implicit explicit)

(implicit) new
Java
(implicit) Java
Java
Java
Java
(implicit)

(explicit)
java.lang.Class forName()
java.lang.ClassLoader loadClass()

Java

Java

Class
Class ClassLoader
ClassLoader
forName
forName loadClass
loadClass
new
new

: Class.forName()

Office.javaWord.java Excel.java
(

9
) Access.java PowerPoint.java
Office.java if else
:
?

:Assemblyjava
public interface Assembly
{
public void start() ;
}

:Office.java
public class Office
{
public static void main(String args[]) throws Exception
{
Class c = Class.forName(args[0]) ;
Object o = c.newInstance() ;
Assembly a = (Assembly) o ;
a.start() ;
}
}

:Word.java
public class Word implements Assembly
{
public void start()
{
System.out.println("Word starts") ;
}
}

:Excel.java
public class Excel implements Assembly
{
public void start()

10
{
System.out.println("Excel starts") ;
}
}

Office.java :
java Office Word java Office Excel
:

java Office Access


( Access.java):

Access(Access.java)
PowerPoint(PowerPoint.java) Assembly

11
(Office.java):
:Access.java
public class Access implements Assembly
{
public void start()
{
System.out.println("Access starts") ;
}
}

:PowerPoint.java
public class PowerPoint implements Assembly
{
public void start()
{
System.out.println("PowerPoint starts") ;
}
}

JDBC Class.forName()
JDBC JDBC (JDBC Software driver)

: verbose:class
Assembly.class interface class
(.class)

C B
I B A :

Java 2 SDK Class


forName()(
):
public static Class forName(String className)

12
:
public static Class forName(String name, boolean initialize,
ClassLoader loader)
forName0():
private static native Class forName0(String name, boolean initialize,
ClassLoader loader) throws ClassNotFoundException;
forName():
forName0(className, true, ClassLoader.getCallerClassLoader());
forName():
forName0(name, initialize, loader);

initialize :
:Office.java
public class Office
{
public static void main(String args[]) throws Exception
{
Class c = Class.forName(args[0],true,null) ;
Object o = c.newInstance() ;
Assembly a = (Assembly) o ;
a.start() ;
}
}

java Office Word


:

null
:
:Office.java
public class Office
{

13
public static void main(String args[]) throws Exception
{
Office off = new Office() ;
Class c = Class.forName(args[0],true,off.getClass().getClassLoader()) ;
Object o = c.newInstance() ;
Assembly a = (Assembly) o ;
a.start() ;
}
}

forName()
forName()
ClassLoader.getCallerClassLoader()
(
ClassLoader.getCallerClassLoader() private
Office Office
)
forName()? :
:Office.java
public class Office
{
public static void main(String args[]) throws Exception
{
Office off = new Office() ;
System.out.println("") ;
Class c = Class.forName(args[0],true,off.getClass().getClassLoader()) ;
System.out.println("") ;
Object o = c.newInstance() ;
Object o2 = c.newInstance() ;
}
}
Word.java :

14
:Word.java
public class Word implements Assembly
{
static
{
System.out.println("Word static initialization") ;
}
public void start()
{
System.out.println("Word starts") ;
}
}

:
java Office Word
:

forName() false:
:Office.java
public class Office
{
public static void main(String args[]) throws Exception
{
Office off = new Office() ;
System.out.println("") ;
Class c = Class.forName(args[0],false,off.getClass().getClassLoader()) ;
System.out.println("") ;
Object o = c.newInstance() ;
Object o2 = c.newInstance() ;
}
}
:

15
true false ?
Java (static initialization block)

newInstance()

: new
forName()+
forName()
false

:
Java
Object Object getClass()
Class
(Class.class) Class
private Class (.class)

Class :

16
Class.class

X.class
Class.class
X.class
X.class

X.class

Class.class
Y.class
Y.class
Y.class

Class
( fieldmethod )
Java Reflection Class
Class Class
(native method)
Java (ClassLoader )
Class ClassLoader
(: null
(bootstrap loader, root loader)
Java )
:

17
,Class.class,ClassLoader

X.class
Class.class
Class.class
(X.class)
X.class
ClassLoader

Class.class
Class.class
Y.class
(Y.class)

Z.class ClassLoader

Z.class Class.class
Class.class
(Z.class)
Z.class

ClassLoader

Class getClassLoader()
getClassLoader()
getClassLoader0():
private native ClassLoader getClassLoader0();
ClassLoader loadClass()
:
:Office.java
public class Office
{
public static void main(String args[]) throws Exception
{
Office off = new Office() ;
System.out.println("") ;
ClassLoader loader = off.getClass().getClassLoader() ;
Class c = loader.loadClass(args[0]) ;
System.out.println("") ;
Object o = c.newInstance() ;
Object o2 = c.newInstance() ;
}
}

18
java Office Word
:

ClassLoader loadClass()

Class forName() false


:
:Office.java
public class Office
{
public static void main(String args[]) throws Exception
{
Class cb = Office.class ;
System.out.println("") ;
ClassLoader loader = cb.getClassLoader() ;
Class c = loader.loadClass(args[0]) ;
System.out.println("") ;
Object o = c.newInstance() ;
Object o2 = c.newInstance() ;
}
}
Office.class
Office getClass()

? Java
java.net.URLClassLoader :
:Office.java
import java.net.* ;

19
public class Office
{
public static void main(String args[]) throws Exception
{
URL u = new URL("file:/d:/my/lib/") ;
URLClassLoader ucl = new URLClassLoader(new URL[]{ u }) ;
Class c = ucl.loadClass(args[0]) ;
Assembly asm = (Assembly) c.newInstance() ;
asm.start() ;
}
}
java.net.URLClassLoader
URLClassLoader
URL
URL
( JAR )
URLClassLoader d:\my\lib\
URL file:/d:/my/lib/( public
static void main(String arga[])) URL
file:lib/
:
:Office.java
import java.net.* ;
public class Office
{
public static void main(String args[]) throws Exception
{
URL u = new URL("file:/d:/my/lib/") ;
URLClassLoader ucl = new URLClassLoader(new URL[]{ u }) ;
Class c = ucl.loadClass(args[0]) ;
Assembly asm = (Assembly) c.newInstance() ;
asm.start() ;

URL u1 = new URL("file:/d:/my/lib/") ;


URLClassLoader ucl1 = new URLClassLoader(new URL[]{ u1 }) ;
Class c1 = ucl1.loadClass(args[0]) ;
Assembly asm1 = (Assembly) c1.newInstance() ;
asm1.start() ;

20
}
}
:

URLClassLoader

!
:

?
:
:Office.java
import java.net.* ;
public class Office
{
public static void main(String args[]) throws Exception

21
{
URL u = new URL("file:/d:/my/lib/") ;
URLClassLoader ucl = new URLClassLoader(new URL[]{ u }) ;
Class c = ucl.loadClass(args[0]) ;
Assembly asm = (Assembly) c.newInstance() ;
asm.start() ;

URL u1 = new URL("file:/d:/my/lib/") ;


URLClassLoader ucl1 = new URLClassLoader(new URL[]{ u1 }) ;
Class c1 = ucl1.loadClass(args[0]) ;
Assembly asm1 = (Assembly) c1.newInstance() ;
asm1.start() ;

System.out.println(Office.class.getClassLoader()) ;
System.out.println(u.getClass().getClassLoader()) ;
System.out.println(ucl.getClass().getClassLoader()) ;
System.out.println(c.getClassLoader()) ;
System.out.println(asm.getClass().getClassLoader()) ;

System.out.println(u1.getClass().getClassLoader()) ;
System.out.println(ucl1.getClass().getClassLoader()) ;
System.out.println(c1.getClassLoader()) ;
System.out.println(asm1.getClass().getClassLoader()) ;
}
}
:

Office.class AppClassLoader( System

22
Loader)URL.class URLClassLoader.class Bootstrap
Loader (: null Java
Bootstrap Loader
Java C++ Java
Bootstrap Loader ) Word.class
URLClassLoader Assembly.class
AppClassLoader (Polymorphism)
(Word.class)
Interface new
getClassLoader()
getClassLoader() Interface
:
Assembly.class.getClassLoader()

Assembly.class AppClassLoader

Bootstrap Loader :

(classloader hierarchy)
()

Java (.class
)( public static void main(String arga[])
)
Java
java xxx.class java.exe
JRE(Java Runtime Environment) JRE
jvm.dll( Java ) Java


Bootstrap
LoaderBootstrap Loader C++( Java

23
Bootstrap Loader Java
null) Bootstrap
Loader
sun.misc Launcher.java
ExtClassLoader( inner class
Launcher$ExtClassLoader.class) Parent null
Bootstrap Loader Bootstrap Loader sun.misc
Launcher.java AppClassLoader( inner class
Launcher$AppClassLoader.class) Parent
ExtClassLoader
Launcher$ExtClassLoader.class Launcher$AppClassLoader.class
Bootstrap Loader Parent
:

Bootstrap Loader Parent


(c++) Extended Loader
(Java)

Parent
System Loader
(Java)

AppClassLoader Sun (System


Loader) AppClassLoader
AppClassLoader
xxx.class( : xxx.class ExtClassLoader Bootstrap
Loader ) Java
:

24
:java xxx.class


JRE
JRE AppClassLoader


jvm.dll
jvm.dll ExtClassLoader

JVM
JVM


Bootstrp Loader

Bootstrap Loader ExtClassLoader AppClassLoader


:
:test.java
public class test
{
public static void main(String args[])
{
ClassLoader cl = test.class.getClassLoader() ;
System.out.println(cl) ;
ClassLoader cl1 = cl.getParent() ;
System.out.println(cl1) ;
ClassLoader cl2 = cl1.getParent() ;
System.out.println(cl2) ;
}
}
:

:
cl.getClass().getClassLoader() cl1.getClass().getClassLoader()

25
null Bootstrap Loader
(
Bootstrap Loader Java
) Parent Parent
:

AppClassLoader

Parent

Bootstrp Loader getClassLoader( )

ExtClassLoader
null
Parent

AppClassLoader ExtClassLoader
URLClassLoader URLClassLoader
URL
AppClassLoader URL java.class.path
java.class.path java.exe cp
-classpath CLASSPATH :
:test.java
public class test
{
public static void main(String args[])
{
String s = System.getProperty("java.class.path");
System.out.println(s) ;
}
}
:

26
AppClassLoader
.()-classpath (-cp )
AppClassLoader -classpath
CLASSPATH CLASSPATH -classpath
-classpath CLASSPATH -classpath

ExtClassLoader
java.ext.dirs:
:test.java
public class test
{
public static void main(String args[])
{
String s = System.getProperty("java.ext.dirs");
System.out.println(s) ;
}
}
:

27
java.ext.dirs java.exe
JRE \lib\ext java.ext.dirs
:

Bootstrap Loader
sun.boot.class.path Bootstrap Loader
:
:test.java
public class test
{
public static void main(String args[])
{
String s = System.getProperty("sun.boot.class.path");
System.out.println(s) ;
}
}
:

sun.boot.class.path :

28

java.class.path sun.boot.class.path
AppClassLoader Bootstrap Loader ( JAR )
AppClassLoader Bootstrap Loader
JAR
ExtClassLoader java.ext.dirs
JAR classes (
-Dsun.boot.class.path=c:\winnt
c:\winnt
JAR )
classpath / -cp / CLASSPATH
AppClassLoader Djava.ext.dirs
ExtClassLoader
Dsun.boot.class.path Bootstrap Loader
AppClassLoader ExtClassLoader
AppClassLoader
ExtClassLoader
; sun.boot.class.path Bootstrap
Loader Bootstrap Loader
JDK 1.4.x <
>\hotspot\src\share\vm\runtime\os.cpp
os::set_boot_path :
static const char classpathFormat[] =
"%/lib/rt.jar:"
"%/lib/i18n.jar:"
"%/lib/sunrsasign.jar:"
"%/lib/jsse.jar:"
"%/lib/jce.jar:"
"%/lib/charsets.jar:"
"%/classes";
JDK 1.4.x JDK 1.3.x ( jsse.jar jce.jar)
1.4.x JDKsun.boot.class.path :

29
AppClassLoader ExtClassLoader

System.setProperty()
AppClassLoader ExtClassLoader

java.class.path
AppClassLoader
:test.java
public class test
{
public static void main(String args[])
{
String old = System.getProperty("java.class.path");
System.out.println(old) ;

System.setProperty("java.class.path","d:\\test");
String s = System.getProperty("java.class.path");
System.out.println(s) ;

testlib tl = new testlib() ;


tl.print() ;
}
}
:testlib.java
public class testlib
{
public void print()
{
System.out.println("I get loaded") ;
}

30
}
(: test.java testlib.java )
:

d:\test testlib.class
testlib.class
testlib.class
AppClassLoader java.class.path
java.ext.dirs ExtClassLoader

Bootstrap Loader
sun.misc
Launcher.java ExtClassLoader Parent null
Bootstrap Loader sun.misc Launcher.java
AppClassLoader Parent ExtClassLoader
:

AppClassLoader

Parent

Bootstrp Loader

null ExtClassLoader

Parent


Parent

31
Parent

: test.java testlib.java:
:test.java
public class test
{
public static void main(String args[])
{
System.out.println(test.class.getClassLoader()) ;
testlib tl = new testlib() ;
tl.print() ;
}
}
:testlib.java
public class testlib
{
public void print()
{
System.out.println(this.getClass().getClassLoader()) ;
}
}

test.class testlib.class <JRE


>\classes ()<JRE
>\lib\ext\classes(
):

32
d:\my :

33
d:\my java test (
)
test.class testlib.class
()

:
<JRE >\classes
test.class
testlib.class
<JRE >\lib\ext\classes
test.class
testlib.class
d:\my
test.class
testlib.class
:

AppClassLoader test.class
Parent ExtClassLoader ExtClassLoader Parent
Bootstrap Loader test.class<JRE >\classes

34
Bootstrap Loader Bootstrap Loader
test.class test.class testlib.class
test.class Bootstrap Loader testlib.class
Bootstrap Loader testlib.class Bootstrap
Loader
test.class testlib.class Bootstrap Loader(null)

:
<JRE >\classes
test.class

<JRE >\lib\ext\classes
test.class
testlib.class
d:\my
test.class
testlib.class
:

AppClassLoader test.class
Parent ExtClassLoader ExtClassLoader Parent
Bootstrap Loader test.class<JRE >\classes
Bootstrap Loader Bootstrap Loader
test.class test.class testlib.class
test.class Bootstrap Loader testlib.class
Bootstrap Loader Bootstrap Loader
testlib.class() Bootstrap Loader Parent
testlib.clss
test.class Bootstrap Loader
NoClassDefFoundError testlib.class
Context Class Loader

:
35
<JRE >\classes

testlib.class
<JRE >\lib\ext\classes
test.class
testlib.class
d:\my
test.class
testlib.class
:

AppClassLoader test.class
Parent ExtClassLoader ExtClassLoader Parent
Bootstrap Loader test.class Bootstrap Loader
test.class() ExtClassLoader
ExtClassLoader <JRE >\lib\ext\classes
test.class test.class testlib.class
test.class ExtClassLoader testlib.class
ExtClassLoader ExtClassLoader
Parent Bootstrap Loader testlib.class
Bootstrap Loader Bootstrap Loader
test.class ExtClassLoader testlib.class
Bootstrap Loader(null)

:
<JRE >\classes

<JRE >\lib\ext\classes
test.class
testlib.class
d:\my
test.class
testlib.class
:

36
AppClassLoader test.class
Parent ExtClassLoader ExtClassLoader Parent
Bootstrap Loader test.class Bootstrap Loader
test.class() ExtClassLoader
ExtClassLoader <JRE >\lib\ext\classes
test.class test.class testlib.class
test.class ExtClassLoader testlib.class
ExtClassLoader ExtClassLoader Parent
Bootstrap Loader testlib.class()
ExtClassLoader ExtClassLoader <JRE
>\lib\ext\classes testlib.class
test.class testlib.clas ExtClassLoader

:
<JRE >\classes

<JRE >\lib\ext\classes
test.class

d:\my
test.class
testlib.class
:

AppClassLoader test.class
Parent ExtClassLoader ExtClassLoader Parent
Bootstrap Loader test.class Bootstrap Loader
test.class() ExtClassLoader
ExtClassLoader <JRE >\lib\ext\classes

37
test.class test.class testlib.class
test.class ExtClassLoader testlib.class
ExtClassLoader ExtClassLoader Parent
Bootstrap Loader testlib.class()
ExtClassLoader ExtClassLoader
testlib.class
test.class ExtClassLoader
NoClassDefFoundError testlib.class
ExtClassLoader testlib.class
Djava.ext.dirs=<> ExtClassLoader
classes JAR
Context Class Loader

:
<JRE >\classes

<JRE >\lib\ext\classes

testlib.class
d:\my
test.class
testlib.class
:

AppClassLoader test.class
Parent ExtClassLoader ExtClassLoader Parent
Bootstrap Loader test.classBootstrap Loader
test.class( ) ExtClassLoader
ExtClassLoader <JRE >\lib\ext\classes
test.class AppClassLoader
test.class test.class testlib.class test.class
AppClassLoader testlib.class AppClassLoader
AppClassLoader Parent
Bootstrap Loader testlib.class()

38
ExtClassLoader ExtClassLoader <JRE
>\lib\ext\classes testlib.class
test.class AppClassLoader testlib.class
ExtClassLoader

:
<JRE >\classes

<JRE >\lib\ext\classes

d:\my
test.class
testlib.class
:

AppClassLoader test.class
Parent ExtClassLoader ExtClassLoader Parent
Bootstrap Loader test.class Bootstrap Loader
test.class() ExtClassLoader
test.class() AppClassLoader
test.class test.class testlib.class
test.class AppClassLoader testlib.class
AppClassLoader Bootstrap Loader
testlib.class( ) ExtClassLoader
testlib.class( ) AppClassloader
AppClassLoader testlib.class
test.class testlib.class AppClassLoader

: Parent
test.class
Bootstrap Loader AppClassLoader Bootstrap
Loader JDBC API

39
JDBC API () Bootstrap
Loader ExtClassLoader JDBC driver
ExtClassLoader AppClassLoader JDBC API
Bootstrap Loader JDBC driver ExtClassLoader
JDBC ?
JDBC Java API(Application Programming
Interface( Bootstrap Loader )
( ExtClassLoader )) SPI(Service Provide Interface
( ExtClassLoader )(
AppClassLoader )) JDBC
JNDI Context Class Loader


:Office.java
import java.net.* ;
public class Office
{
public static void main(String args[]) throws Exception
{
URL u = new URL("file:/d:/my/lib/") ;
URLClassLoader ucl = new URLClassLoader(new URL[]{ u }) ;
Class c = ucl.loadClass(args[0]) ;
Assembly asm = (Assembly) c.newInstance() ;
asm.start() ;

URL u1 = new URL("file:/d:/my/lib/") ;


URLClassLoader ucl1 = new URLClassLoader(new URL[]{ u1 }) ;
Class c1 = ucl1.loadClass(args[0]) ;
Assembly asm1 = (Assembly) c1.newInstance() ;
asm1.start() ;

System.out.println(Office.class.getClassLoader()) ;
System.out.println(u.getClass().getClassLoader()) ;
System.out.println(ucl.getClass().getClassLoader()) ;
System.out.println(c.getClassLoader()) ;
System.out.println(asm.getClass().getClassLoader()) ;

System.out.println(u1.getClass().getClassLoader()) ;

40
System.out.println(ucl1.getClass().getClassLoader()) ;
System.out.println(c1.getClassLoader()) ;
System.out.println(asm1.getClass().getClassLoader()) ;

System.out.println(Assembly.class.getClassLoader()) ;
}
}

Java :

Bootstrp Loader

null Parent

ExtClassLoader

Parent
Parent
URLClassLoader
AppClassLoader

Parent

URLClassLoader

: java office Word


AppClassLoader Office.class
Parent(ExtClassLoader) Parent Parent(Bootstrap Loader)
Office.class AppClassLoader
Office.class Office.class URL URLClassLoader

ClassLoader.getCallerClassLoader() (
ClassLoader.getCallerClassLoader() private
new )
AppClassLoader Parent(ExtClassLoader) Parent
Parent(Bootstrap Loader)Bootstrap Loader <JRE
>\lib\rj.jar URL.class URLClassLoader.class
Bootstrap Loader URLClassLoader
lib\ Word.class Word.class

41
Assembly.class URLClassLoader Parent(AppClassLoader)
Parent Parent(ExtClassLoader) Parent Parent Parent(Bootstrap
Loader) AppClassLoader Assembly.class
URLClassLoader Word.class Assembly.class (asm
asm1) Word.class (c c1)
URLClassLoader Assembly.getClassLoader()
AppClassLoader :

Word.class AppClassLoader (
office.class )

42
URLClassLoader AppClassLoader
AppClassLoader Word.class
Word.class AppClassLoader


?
:

Bootstrp Loader

Parent
ExtClassLoader

Parent
AppClassLoader
www.xxx.com
www.sun.com
Parent Parent

URLClassLoader URLClassLoader

URLClassLoader
URLClassLoader AppClassLoader
ExtClassLoader Bootstrap Loader (
+)

43
(
Java )

www.sun.com
www.sun.com www.xxx.com

Java
? Java

(Java Sun Microsystems


(Gong Li))

OReilly
Java Security 2/e :
:
http://www.oreilly.com.tw/chinese/java/java_security2.html

:
http://www.oreilly.com/catalog/javasec2/

44
45