How does the java compiler find the class files whereas the classpath is not set to the jdk path?

可紊 提交于 2019-12-01 04:00:25

So I was expecting a compiling error due to the fact that the compiler would not be able to find the java.lang.String class file.

The short answer is that the compiler knows where to find all of the standard Java SE library classes without you telling it.

The longer answer is that String class is being found on the bootclasspath. This is implicitly set by the javac command to refer to the relevant JARs in the JDK installation. The javac command searches the bootclasspath before it looks for stuff on the regular classpath.

The classpath variable doesn't do what you think. To cite the oracle documentation:

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.)

Source: http://docs.oracle.com/javase/tutorial/essential/environment/paths.html

Basically since java.lang.* is part of the platform and delivered with the JDK/JRE, the compiler doesn't have to be told by you where to look for them.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!