How Classloader determines which classes it can load?

那年仲夏 提交于 2019-12-03 16:31:53

That differs depending on the implementation of the classloader. But all Classes a ClassLoader can load are retrieved by ClassLoader.findClass(String)

There are many implementations but the most common one is the URLClassLoader which loads classes from directories and jar files.

The classloader checks all classes (java class files) within your CLASSPATH path variable. If your class is found there, it exists, otherwise it doesn't.

So practically, your /src directory and all subdirectories (=packages) are scanned.

The classloader transforms the requested class name into a file name and then tries to find a "class file" of that name from a file system. As @poitroae notes, it uses the CLASSPATH variable, if set, as a starting place. Most IDEs and such extend this to include your working directories for the project.

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