Search code examples
javamavenintellij-ideaclassloader

How could possibly a project runs well when its Classpath is lack of some classes used in it?


It's interesting to find out that MultiTenantConnectionProvider.java doesn't exists in my project, but my project can be maven build and runs well.

screenshot of missing class.

I use Intellij Idea 2016.2. As the class is packed in mysql-connector-java-5.1.31.jar. So I tried to connect to mysql db and query out some data to test if it works well. It turns out that the missing of class doesn't influence the project runs well. It seems unlogical, isn't it?

data can be quired out


Solution

  • Not at all. Most JVM implementations do lazy classloading. This means: a class doesn't get loaded by the class loader until it is really needed; for example when some other code instantiates an object of such a class.

    In other words: when your application is not throwing a ClassNotFoundException resp. a NoClassDefFoundError ... the conclusion is: the code paths that get executed simply do not require that one class to be loaded.

    Like in: you might notice that you lost your keys at the party up to the point when you are back home and try to open the front door.

    In other words: if your program builds and runs without that class, then that class isn't used much.