view plaincopy to clipboardprint? package classloader; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ButtonTest { public static void main(String[] args) { ButtonFrame frame = new ButtonFrame(); frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } /** A frame with a button panel */ class ButtonFrame extends JFrame { public ButtonFrame() { setTitle("ButtonTest"); setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); // add panel to frame ButtonPanel panel = new ButtonPanel(); add(panel); } public static final int DEFAULT_WIDTH = 300; public static final int DEFAULT_HEIGHT = 200; } /** A panel with three buttons. */ class ButtonPanel extends JPanel { public ButtonPanel() { // create buttons JButton yellowButton = new JButton("Yellow"); JButton blueButton = new JButton("Blue"); JButton redButton = new JButton("Red"); // add buttons to panel add(yellowButton); add(blueButton); add(redButton); // create button actions ColorAction yellowAction = new ColorAction(Color.YELLOW); ColorAction blueAction = new ColorAction(Color.BLUE); ColorAction redAction = new ColorAction(Color.RED); // associate actions with buttons yellowButton.addActionListener(yellowAction); blueButton.addActionListener(blueAction); redButton.addActionListener(redAction); } /** An action listener that sets the panel's background color. */ private class ColorAction implements ActionListener { public ColorAction(Color c) { backgroundColor = c; } public void actionPerformed(ActionEvent event) { setBackground(backgroundColor); } private Color backgroundColor; } } package classloader; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ButtonTest { public static void main(String[] args) { ButtonFrame frame = new ButtonFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } /** A frame with a button panel */ class ButtonFrame extends JFrame { public ButtonFrame() { setTitle("ButtonTest"); setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); // add panel to frame ButtonPanel panel = new ButtonPanel(); add(panel); } public static final int DEFAULT_WIDTH = 300; public static final int DEFAULT_HEIGHT = 200; } /** A panel with three buttons. */ class ButtonPanel extends JPanel { public ButtonPanel() { // create buttons JButton yellowButton = new JButton("Yellow"); JButton blueButton = new JButton("Blue"); JButton redButton = new JButton("Red"); // add buttons to panel add(yellowButton); add(blueButton); add(redButton); // create button actions ColorAction yellowAction = new ColorAction(Color.YELLOW); ColorAction blueAction = new ColorAction(Color.BLUE); ColorAction redAction = new ColorAction(Color.RED); // associate actions with buttons yellowButton.addActionListener(yellowAction); blueButton.addActionListener(blueAction); redButton.addActionListener(redAction); } /** An action listener that sets the panel's background color. */ private class ColorAction implements ActionListener { public ColorAction(Color c) { backgroundColor = c; } public void actionPerformed(ActionEvent event) { setBackground(backgroundColor); } private Color backgroundColor; } }
注意:虚拟机只加载执行一个程序所需要的类文件.举个例子,比如执行MyProgram.class,虚拟机运行的步骤如下: 1,虚拟机有一个加载类文件的机制,比如,从硬盘读取文件或者就网络获得;虚拟机用这个机制加载MyProgram的类文件 2,如果MyProgram有一个实例变量或者是超类,那么实例变量和超类的类文件也被加载. (加载一个类所依赖的所有类的过程叫做resolving the class-->自己理解吧) 3,然后虚拟机执行MyProgram的main方法(因为是静态方法,所以不需要new MyProgram的实例) 4,如果main 方法或者main方法调用的方法需要其他的类的话,这些类也被加载.
类加载机制不是仅仅用一个类加载器,任何一个java程序至少有以下三个类加载器(为了不影响大家的理解,这里我就不翻译这三个类加载器的名称了) The bootstrap class loader:加载系统类(有代表性的,jdk的rt.jar里的类),他是虚拟机的必要组成部分,并且一般是用C实现的. 也有类加载器对象(就是指具体的一个类加载器)不关联bootstrap class loader,比如String.class.getClassLoader()返回null.
The extension class loader:加载jre/lib/ext目录下的class,你可以把你的jar文件放到这个目录,extension class loader 将会加载到jar里面的类,即使你不设置classpath.(一些人建议使用这个机制以让你不受classpath的烦扰,不过注意以下的警告)
The system class loader (有时也叫应用程序加载器):加载应用程序类. 他主要加载classpath目录和jar/zip文件里的class,通过设置CLASSPATH环境变量或者是运行java的时候用[-classpath]选项指定classpath
在SUN的java实现里,the extension and system class loaders都是用java实现的,他们都是URLClassLoader类的实例.
警告:如果你把jar文件放到jre/lib/ext目录下,并且你的jar文件中的类需要加载一个不是system or extension的类的话, 你将遇到麻烦.扩展类加载器不使用类路径.如果你想把类放到jre/lib/ext下进行管理的话,请牢记这一点. ==>怎么理解这一点:也就是说如果你把自己的x.jar放到jre/lib/ext下的话,如果你自己的x.jar里的class要用到不在 x.jar里也不在jre/lib/ext的类的话,会导致类加载不了.不难想象吧,因为你x.jar里的类是由extension class loader 加载的,他不会加载classpath路径下的类.
警告:把jar文件放到jre/lib/ext目录下,还有第二个缺陷:有时侯,程序员忘记了他很久以前放在这个目录下的类文件. 当class loader似乎忽略了类路径(其实没有,因为类加载总是先让父的类加载器加载类,只有父的类加载器加载不了的话 才由自己来加载,"extension class loader是system class loader的父,因此..."), 而加载了放在扩展目录下的遗忘已久的类的时候,他们就会迷惑不解.
class loader有父子关系,bootstrap class loader以外的每一个class loader都有一个父的类加载器. 类加载器会给父的加载器一个机会加载任何给定的类,如果父加载器加载失败的话自身才去加载. 举个例子,当系统class loader被要求加载一个系统类的时候(比如,java.util.ArrayList), 那么,首先需要extension class loader加载,而extension class loader又先让bootstrap class loader, 最终由bootstrap class loader查找并且加载了rt.jar,其他任何类加载器不需要再搜索.
这种情况下.库类需要搜索应用程序类加载器(代码如下): Thread t = Thread.currentThread(); ClassLoader loader = t.getContextClassLoader(); Class cl = loader.loadClass(className);
Using Class Loaders as Namespaces 任何一个java程序员都知道包名是用来消除名字冲突的.在标准类库里有两个叫Date的类(java.util.Date and java.sql.Date). 简单的名字(这里指的是你在程序里直接写Date)只是程序员方便,并且需要包含import语句.在一个运行的程序中, 所有的class都包含他们的包名.
这也许让你吃惊,然而,在同一个虚拟机里面你可以有两个类名和包名都相同的类.一个类是通过他的全名和类加载器来标识的. This technique is useful for loading code from multiple sources.比如,浏览器为每个web页面使用单独的 applet class loader.这允许虚拟机分开来自不同web页面的类,不管他们是怎么命名的.
Writing Your Own Class Loader The loadClass method of the ClassLoader superclass takes care of the delegation to the parent and calls findClass only if the class hasn't already been loaded and if the parent class loader was unable to load the class. 定义自己的类加载器只需要继承ClassLoader类并且重写findClass(String className)方法. ClassLoader父类的loadClass方法负责授权给父的类加载器 并且只有在还没有加载并且父的类加载器不能加载的时候 才调用findClass方法.