如果我的 CLASSPATH 变量为空,我的 Java 代码是如何编译的?

How is my Java code compiled if my CLASSPATH variable is empty?

我知道 $CLASSPATH 包含一个路径列表,Java 编译器在其中查找 jar 文件以使用它们的 classes。

当我键入:echo $CLASSPATH 我得到一个空行,这意味着 CLASSPATH 是空的。

现在,我写了下面的代码:

import java.util.ArrayList;

public class Main {

    public static void main(String[] args) {
    ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("Hello");
        System.out.println(arrayList);        
    }
}

并用命令编译:

javac Main.java

文件编译成功。如果 CLASSPATH 为空,java 编译器如何找到 class java.util.ArrayList

来自 Oracle 文档:

The CLASSPATH variable is one way to tell applications, including the JDK tools, where to look for user classes. (Classes that are part of the JRE, JDK platform, and extensions should be defined through other means, such as the bootstrap class path or the extensions directory.)

所以,classpath 实际上是给用户 类 而 ArrayList 是 JDK 的一部分。另外:

The default value of the class path is ".", meaning that only the current directory is searched

有关完整说明,请查看此处:java paths