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會從最前面開始查找,找不到的情況下 纔會繼續查找後面的部分。

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