How Classes are Found
转载自: https://docs.oracle.com/javase/7/docs/technotes/tools/findingclasses.html
How Classes are Found |
SDK Tools |
- How the Java Launcher Finds ClassesHow Javac and Javadoc Find Classes
- Class Loading and Security Policies
How the Java Launcher Finds Classes
The Java launcher, java, initiates the Java virtual machine. The virtual machine searches for and loads classes in this order:
- Bootstrap classes - Classes that comprise the Java platform, including the classes in
rt.jarand several other important jar files.- Extension classes - Classes that use the Java Extension mechanism. These are bundled as
.jarfiles located in the extensions directory.- User classes - Classes defined by developers and third parties that do not take advantage of the extension mechanism. You identify the location of these classes using the -classpath option on the command line (the preferred method) or by using the CLASSPATH environment variable. (See Setting the Classpath for Windows or Solaris.)
In effect, these three search paths are joined to form a simple class path. This is similar to the "flat" class path previously used, but the current model has some important differences:
- It is relatively difficult to accidentally "hide" or omit the bootstrap classes.
- In general, you only have to specify the location of user classes. Bootstrap classes and extension classes are found "automatically".
- The tools classes are now in a separate archive (tools.jar) and can only be used if included in the user class path (to be explained shortly).
How the Java Launcher Finds Bootstrap Classes
Bootstrap classes are the classes that implement the Java 2 Platform. Bootstrap classes are in the
rt.jarand several other jar files in thejre/libdirectory. These archives are specified by the value of the bootstrap class path which is stored in thesun.boot.class.pathsystem property. This system property is for reference only, and should not be directly modified.It is very unlikely that you will need to redefine the bootstrap class path. The nonstandard option, -Xbootclasspath, allows you to do so in those rare cicrcumstances in which it is necessary to use a different set of core classes.
Note that the classes which implement the Java 2 SDK tools are in a separate archive from the bootstrap classes. The tools archive is the SDK's
/lib/tools.jarfile. The development tools add this archive to the user class path when invoking the launcher. However, this augmented user class path is only used to execute the tool. The tools that process source code, javac and javadoc, use the original class path, not the augmented version. (For more information, see How Javac and Javadoc Find Classes, below.)How the Java Launcher Finds Extension Classes
Extension classes are classes which extend the Java platform. Every
.jarfile in the extension directory, jre/lib/ext, is assumed to be an extension and is loaded using the Java Extension Framework. Loose class files in the extension directory will not be found. They must be contained in a .jar file (or .zip file). There is no option provided for changing the location of the extension directory.If the jre/lib/ext directory contains multiple
.jarfiles, and those files contain classes with the same name, such as:
smart-extension1_0.jar contains class smart.extension.Smart
smart-extension1_1.jar contains class smart.extension.Smartthe class that actually gets loaded is undefined.
How the Java Launcher Finds User Classes
User classes are classes which build on the Java platform. To find user classes, the launcher refers to the user class path -- a list of directories, JAR archives, and ZIP archives which contain class files.
A class file has a subpath name that reflects the class's fully-qualified name. For example, if the class
com.mypackage.MyClassis stored under/myclasses, then/myclassesmust be in the user class path and the full path to the class file must be /myclasses/com/mypackage/MyClass.class. If the class is stored in an archive namedmyclasses.jar, thenmyclasses.jarmust be in the user class path, and the class file must be stored in the archive ascom/mypackage/MyClass.class.The user class path is specified as a string, with a colon (
:) separating the class path entries on Solaris, and a semi-colon (;) separating entries on Microsoft Windows systems. The java launcher puts the user class path string in thejava.class.pathsystem property. The possible sources of this value are:
- The default value, "
.", meaning that user class files are all the class files in the current directory (or under it, if in a package).- The value of the CLASSPATH environment variable, which overrides the default value.
- The value of the -cp or -classpath command line option, which overrides both the default value and the CLASSPATH value.
- The JAR archive specified by the -jar option, which overrides all other values. If this option is used, all user classes must come from the specified archive.
How the Java Launcher Finds JAR-class-path Classes
A JAR file usually contains a "manifest" -- a file which lists the contents of the JAR. The manifest can define a JAR-class-path, which further extends the class path (but only while loading classes from that JAR). Classes accessed by a JAR-class-path are found in the following order:
- In general, classes referenced by a JAR-class-path entry are found as though they were part of the JAR file. The JAR files that appear in the JAR-class-path are searched after any earlier class path entries, and before any entries that appear later in the class path.
- However, if the JAR-class-path points to a JAR file that was already searched (for example, an extension, or a JAR file that was listed earlier in the class path) then that JAR file will not be searched again. (This optimization improves efficiency and prevents circular searches.) Such a JAR file is searched at the point that it appears, earlier in the class path.
- If a JAR file is installed as an extension, then any JAR-class-path it defines is ignored. All the classes required by an extension are presumed to be part of the SDK or to have themselves been installed as extensions.
How Javac and JavaDoc Find Classes
The javac and javadoc tools use class files in two distinct ways:
- Like any Java application, javac and javadoc must load various class files in order to run.
- To process the source code they operate on, javac and javadoc must obtain information on object types used in the source code.
The class files used to resolve source code references are mostly the same class files used to run javac and javadoc. But there are some important exceptions:
- Both javac and javadoc often resolve references to classes and interfaces that having nothing to do with the implementation of javac or javadoc. Information on referenced user classes and interfaces may be present in the form of class files, source code files, or both.
- The tools classes in
tools.jarare only used to run javac and javadoc. The tools classes are not used to resolve source code references unlesstools.jaris in the user class path.- A programmer may want to resolve boot class or extension class references using an alternative Java platform implementation. Both javac and javadoc support this with their -bootclasspath and -extdirs options. Use of these options does not modify the set of class files used to run the javac or javadoc tools themselves.
If a referenced class is defined in both a class file and source file, javadoc always uses the source file (javadoc never compiles source files). In the same situation javac uses class files, but automatically recompiles any class files it determines to be out of date. The rules for automatic recompilation are documented in the javac document for Windows or Solaris.
By default, javac and javadoc search the user class path for both class files and source code files. If the -sourcepath option is specified, javac and javadoc search for source files only on the specified source file path, while still searching the user class path for class files.
Class Loading and Security Policies
To be used, a class or interface must be loaded by a class loader. Use of a particular class loader determines a security policy associated with the class loader.
A program can load a class or interface by calling the loadClass method of a class loader object. But usually a program loads a class or interface simply by referring to it. This invokes an internal class loader, which can apply a security policy to extension and user classes. If the security policy has not been enabled, all classes are "trusted". Even if the security policy is enabled, it does not apply to bootstrap classes, which are always "trusted."
When enabled, security policy is configured by system and user policy files. The Java 2 SDK includes a system policy file that grants "trusted" status to extension classes and places basic restrictions on user classes.
To enable or configure the security policy, refer to Security Features.
Note: Some security programming techniques that worked with the Java 1.1 platform are incompatible with the class loading model of the Java 2 Platform.
How Classes are Found的更多相关文章
- 代码的坏味道(9)——异曲同工的类(Alternative Classes with Different Interfaces)
坏味道--异曲同工的类(Alternative Classes with Different Interfaces) 特征 两个类中有着不同的函数,却在做着同一件事. 问题原因 这种情况往往是因为:创 ...
- eclipse中的classes文件夹同步问题
问题: 在同步项目时,由于误操作将classes文件夹加入到了同步版本中,这样会导致每次更新程序编译后,会有很多class文件显示在同步清单中. 解决方案: 将classes文件不设置为同步. 1. ...
- Introduction of OpenCascade Foundation Classes
Introduction of OpenCascade Foundation Classes Open CASCADE基础类简介 eryar@163.com 一.简介 1. 基础类概述 Foundat ...
- 6.Configure Domain Classes(配置领域类)【EF Code-First 系列】
在前面的部分中,我们学习了Code-First默认约定,Code-First使用默认的约定,根据你的领域类,然后生成概念模型. Code-First模式,发起了一种编程模式:约定大于配置.这也就是说, ...
- app:clean classes Exception
Error:Execution failed for task ':app:clean'.> Unable to delete directory: C:\Users\LiuZhen\Deskt ...
- Android framework编译出来的jar包classes.jar的位置
在源码环境下编译 Android framework编译出来的jar包classes.jar的位置 out/target/common/obj/JAVA_LIBRARIES/framework_in ...
- yii 核心类classes.php详解(持续更新中...)
classes.php在yii运行的时候将被自动加载,位于yii2文件夹底下. <?php /** * Yii core class map. * * This file is automati ...
- Top 15 Java Utility Classes
In Java, a utility class is a class that defines a set of methods that perform common functions. Thi ...
- Eclipse下无法自动编译,或者WEB-INF/classes目录下没文件,编译失败的解决办法(转载)
文章来源:http://www.cnblogs.com/xfiver/archive/2010/07/07/1772764.html 1. IOException parsing XML docum ...
- [Android] 升级了新的android studio之后 发生如下的报错,The following classes could not be instantiated:
The following classes could not be instantiated:- android.support.v4.widget.DrawerLayout (Open Class ...
随机推荐
- table的 noWrap 属性不换行
nowrap是什么意思? HTML中td元素的nowrap属性表示禁止单元格中的文字自动换行. 但使用时要注意的是,td元素noWrap属性的行为与td元素的width属性有关. td元素中nowra ...
- MySQL 水平拆分与垂直拆分详解
前言:说到优化mysql,总会有这么个回答:水平拆分,垂直拆分,那么我们就来说说什么是水平拆分,垂直拆分. 一.垂直拆分 说明:一个数据库由很多表的构成,每个表对应着不同的业务,垂直切分是指按照业务将 ...
- ASP.NET MVC 企业级实战
1.泛型 public class List<T>{ } 当定义泛型类的实例时,必须指定这个实例所存储的实际类型,泛型允许程序员将一个实际的数据类型规约延迟至泛型的实例被创建时才确定,泛型 ...
- Ceph rdb
Ceph 独一无二地用统一的系统提供了对象.块.和文件存储功能,它可靠性高.管理简便.并且是自由软件. Ceph 的强大足以改变公司的 IT 基础架构.和管理海量数据. Ceph 可提供极大的伸缩性— ...
- Vue学习笔记六:v-model 数据双向绑定
目录 v-model简介和适用范围 新建HTML 所见即所得 v-model模拟简易计算器 v-model简介和适用范围 Vue的一大特点之一就是数据的双向绑定,v-model就是实现这个功能的指令, ...
- Python 几个常见函数
本文主要总结常见的函数知识点. 1.zip函数 用来并行迭代,可以把两个序列并在一起,然后返回一个元组的列表 names = ['Ann','Jame','Anla'] ages = [11,12,1 ...
- docker 基础知识分享ppt
给团队做的docker基础分享ppt, 见下面的附件. https://files.cnblogs.com/files/harrychinese/docker_intro.pptx
- 六十一、linux 编程—— 守护进程
61.1 介绍 守护进程(daemon)是生存期长的一种进程.它们常常在系统引导装入时启动,在系统关闭时终止 守护进程也称为后台进程 所有守护进程都以超级用户(用户 ID 为0)的优先权运行. 守护进 ...
- ibatis .net $与#的区别
$与#的区别 SELECT * FROM TABLE WHERE Id = #id# 其中如果字段id为字符串类型,那么#id#表示的就是'id',也就是说会自动加引号.如果id为整型,那么#id#就 ...
- python数字常量
数学常量 pi 数学常量 pi(圆周率,一般以π来表示) e 数学常量 e,e即自然常数(自然常数).