我们都知道Java初始化一个类的时候可以用new 操作符来初始化,
也可通过Class.forName()的方式来得到一个Class类型的实例,然后通过这个Class类型的实例的newInstance 来初始化.
我们把前者叫做JAVA的静态加载,把后者叫做动态加载.后者在很多框架中经常使用,通过用property文件的方式指定类名称.如我们熟悉的在写JDBC代码的时候加载不同数据库的驱动.
静态加载的时候如果在运行环境中找不到要初始化的类,抛出的是NoClassDefFoundError,它在JAVA的异常体系中是一个Error,错误.
动态态加载的时候如果在运行环境中找不到要初始化的类,抛出的是ClassNotFoundException,它在JAVA的异常体系中是一个checked异常,在写代码的时候就需要catch.
下面用实例来演示:
ReferencedCls是需要初始化的类,它在一个单独的jar中,它被StaticReferencingCls和DynamicReferencingCls分别以静态和动态的方式加载.
将prjT1做成一个jar包,t1.jar.将prjT2做成一个jar包,t2.jar.放在d:/temp文件夹.
JAVA代码如下:
- package com.test1;
- public class ReferencedCls {
- private String str = "test value";
- public String getStr() {
- return str;
- }
- public void setStr(String str) {
- this.str = str;
- }
- public ReferencedCls() {
- System.out.println("Constructor: ReferencedCls");
- }
- }
- package com.test2;
- import com.test1.ReferencedCls;
- public class StaticReferencingCls {
- public static void main(String[] args) {
- System.out.println("Started testing static loading...............");
- ReferencedCls cls = new ReferencedCls();
- System.out.println(cls.getStr());
- }
- }
- package com.test2;
- import com.test1.ReferencedCls;
- public class DynamicReferencingCls {
- public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
- System.out.println("Started testing dynamic loading...............");
- Class cls = Class.forName("com.test1.ReferencedCls");
- ReferencedCls obj = (ReferencedCls)cls.newInstance();
- System.out.println(obj.getStr());
- }
- }
以不同的方式运行:
1,在运行环境中能找到需要的类时,都正常运行
- D:\>java -cp d:/temp/t1.jar;d:/temp/t2.jar com.test2.StaticReferencingCls
- Started testing static loading...............
- Constructor: ReferencedCls
- test value
- D:\>java -cp d:/temp/t1.jar;d:/temp/t2.jar com.test2.DynamicReferencingCls
- Started testing dynamic loading...............
- Constructor: ReferencedCls
- test value
2,以静态方式加载,运行环境缺失需要的类,抛出NoClassDefFoundError
- D:\>java -cp d:/temp/t2.jar com.test2.StaticReferencingCls
- Started testing static loading...............
- Exception in thread "main" java.lang.NoClassDefFoundError: com/test1/ReferencedCls
- at com.test2.StaticReferencingCls.main(StaticReferencingCls.java:9)
- Caused by: java.lang.ClassNotFoundException: com.test1.ReferencedCls
- at java.net.URLClassLoader$1.run(Unknown Source)
- at java.security.AccessController.doPrivileged(Native Method)
- at java.net.URLClassLoader.findClass(Unknown Source)
- at java.lang.ClassLoader.loadClass(Unknown Source)
- at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
- at java.lang.ClassLoader.loadClass(Unknown Source)
- at java.lang.ClassLoader.loadClassInternal(Unknown Source)
- ... 1 more
3,以动态方式加载,运行环境缺失需要的类,抛出ClassNotFoundException,同时在写代码的时候也需要catch这个异常或者声明这个异常.
- D:\>java -cp d:/temp/t2.jar com.test2.DynamicReferencingCls
- Started testing dynamic loading...............
- Exception in thread "main" java.lang.ClassNotFoundException: com.test1.ReferencedCls
- at java.net.URLClassLoader$1.run(Unknown Source)
- at java.security.AccessController.doPrivileged(Native Method)
- at java.net.URLClassLoader.findClass(Unknown Source)
- at java.lang.ClassLoader.loadClass(Unknown Source)
- at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
- at java.lang.ClassLoader.loadClass(Unknown Source)
- at java.lang.ClassLoader.loadClassInternal(Unknown Source)
- at java.lang.Class.forName0(Native Method)
- at java.lang.Class.forName(Unknown Source)
- at com.test2.DynamicReferencingCls.main(DynamicReferencingCls.java:11)
4,JVM本身使用的方式也是静态加载,如用JAVA命令运行一个带main方法的类,这个类在运行环境缺失.
- D:\>java com.test2.XXXCls
- Exception in thread "main" java.lang.NoClassDefFoundError: com/test2/XXXCls
- Caused by: java.lang.ClassNotFoundException: com.test2.XXXCls
- at java.net.URLClassLoader$1.run(Unknown Source)
- at java.security.AccessController.doPrivileged(Native Method)
- at java.net.URLClassLoader.findClass(Unknown Source)
- at java.lang.ClassLoader.loadClass(Unknown Source)
- at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
- at java.lang.ClassLoader.loadClass(Unknown Source)
- at java.lang.ClassLoader.loadClassInternal(Unknown Source)
- Could not find the main class: com.test2.XXXCls. Program will exit.
虚拟机会延迟加载类,不是import的类都会被加载,只有在第一次使用的时候才加载。
比如下面的代码,如果不满足条件,XClass可能在整个运行期间都不会被加载。
if("1".equals(args[0])){
System.out.println(XClass.staticStr);
}