我在我的电脑上安装了JDK7。我只是指定了这样的路径:
我没有设置CLASSPATH。
我写了一个测试程序,程序是这样的:
import java.io.File;
public class Hello_1 {
public static void main(String[] args) {
System.out.println(File.separator);
}
}
我这样编译并运行它:
根据结果,我们可以看到它没有任何错误。我很困惑。我没有设置CLASSPATH,根据ORACLE DOC,它说:
The default value of the class path is ".", meaning that only the current directory is searched. Specifying either the CLASSPATH variable or the -cp command line switch overrides this value.
如果我没有设置CLASSPATH变量,默认的类路径是“.”,所以当我编译Hello_1.java时,它只会在当前目录中搜索java.io.File,而没有关于java.io.File的信息,所以它会抛出异常。但为什么效果很好呢?
最佳答案
类路径用于搜索自己的类,但内置类库会自动可用,无需添加到类路径中。因此,Hello_1
在默认类路径(当前目录)中找到,java.io.File
在内置类库中找到。
关于java - 没有设置CLASSPATH变量为什么程序可以运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21308869/