CLASSPATH 官网摘要

原文链接:https://docs.oracle.com/javase/8/docs/technotes/tools/windows/classpath.html

Setting the Class Path

The class path is the path that the Java Runtime Environment (JRE) searches for classes and other resource files.
CLASSPATH 是指JRE用于查找classes和resources文件的位置 (class search path (class path))

Synopsis

Class paths to the JAR, zip or class files. Each class path should end with a file name or directory depending on what you are setting the class path to, as follows:

  1. For a JAR or zip file that contains class files, the class path ends with the name of the zip or JAR file.
    对于一个包含class文件的JAR或者zip文件,classpath设置时,需要指定该文件的名称。

  2. For class files in an unnamed package, the class path ends with the directory that contains the class files.

  3. For class files in a named package, the class path ends with the directory that contains the root package, which is the first package in the full package name.

  • 2、3的基本意思:
    指定路径a作为classpath 将包含以下部分
    1. a中的所有符合package设计的classes(直接递归查找)
    2. 直接目录下的classes文件

Multiple path entries are separated by semicolons(分号) with no spaces around the equals sign (=) in Windows and colons in Oracle Solaris.

The default class path is the current directory. Setting the CLASSPATH variable or using the -classpath command-line option overrides that default, so if you want to include the current directory in the search path, then you must include a dot (.) in the new settings.
默认的CLASSPATH是当前路径。
在命令行中使用-classpath参数,将会覆盖掉这个默认值;
如果需要,在使用-classpath时,手动带上.(当前路径)

Class path entries that are neither directories nor archives (.zip or JAR files) nor the asterisk (*) wildcard character are ignored.
not (dir | .zip | .jar | 通配符) 的内容,则将会be ignored.

Description

The JDK, the JVM and other JDK tools find classes by searching the Java platform (bootstrap) classes, any extension classes, and the class path, in that order. For details about the search strategy, see How Classes Are Found at
http://docs.oracle.com/javase/8/docs/technotes/tools/findingclasses.html
classed的查找顺序: bootstrap -> extension -> CLASSPATH

Class Path Wild Cards

Class path entries can contain the base name wildcard character (*), which is considered equivalent to specifying a list of all of the files in the directory with the extension .jar or .JAR. For example, the class path entry mydir/* specifies all JAR files in the directory named mydir. A class path entry consisting of * expands to a list of all the jar files in the current directory. Files are considered regardless of whether they are hidden (have names beginning with ‘.’).
当一个目录下包含很多.jar文件 并且希望将这些.jar文件都作为CLASSPATH,那么可以通过使用通配符的方式来实现。
如CLASSPATH=libs/*, 即表示将libs目录下的所有的.jar文件都包含在CLASSPATH中(不递归)。

A class path entry that contains an asterisk (*) does not match class files. To match both classes and JAR files in a single directory mydir, use either mydir:mydir/* or mydir/*:mydir. The order chosen determines whether the classes and resources in mydir are loaded before JAR files in mydir or vice versa.
使用通配符的方式,是不包含该路径下的class文件的。即libs/*, 是不会将libs/a.class文件作为CLASSPATH的。
如果同时需要libs下的.jar和.class文件,那么 需要这样表示:libs:libs/* 或者 libs/*:libs (顺序也很重要,下面会提到)。

Subdirectories are not searched recursively. For example, mydir/* searches for JAR files only in mydir, not in mydir/subdir1, mydir/subdir2, and so on.
通配符的方式,不支持递归。即libs/*, 不包含libs/dir/a.jar.

The order in which the JAR files in a directory are enumerated in the expanded class path is not specified and may vary from platform to platform and even from moment to moment on the same machine. A well-constructed application should not depend upon any particular order. If a specific order is required, then the JAR files can be enumerated explicitly in the class path.
使用通配符时,实际上 是会自动枚举指定目录下的.jar文件的, — 但是,这种枚举的顺序 无法确定。

Expansion of wild cards is done early, before the invocation of a program’s main method, rather than late, during the class-loading process. Each element of the input class path that contains a wildcard is replaced by the (possibly empty) sequence of elements generated by enumerating the JAR files in the named directory. For example, if the directory mydir contains a.jar, b.jar, and c.jar, then the class path mydir/* is expanded into mydir/a.jar:mydir/b.jar:mydir/c.jar, and that string would be the value of the system property java.class.path.
CLASSPATH中的通配符,会在程序的main方法调用前就处理完毕,并且 会将展开的结果 已字符串的形式存储在 java.class.path 的系统变量中。

Specification Order

The order in which you specify multiple class path entries is important. The Java interpreter will look for classes in the directories in the order they appear in the class path variable. In the previous example, the Java interpreter will first look for a needed class in the directory C:\java\MyClasses. Only when it does not find a class with the proper name in that directory will the interpreter look in the C:\java\OtherClasses directory.
CLASSPATH中内容的顺序 是重要的。Java interpreter会从最前面开始查找,找不到的情况下 才会继续查找后面的部分。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章