Is a jar file read more than once when the JVM runs an application?

Does the JVM read the .jar file from disk more than once for a long-term program, or is it read only once and the whole image is in memory until the JVM completes?

I was doing a job that took about 26 hours, and towards the end there was an undefined object exception ( NoClassDefFoundError ), which I think was due to the fact that I was making code changes and re- .jar from the development environment (i.e. I created new on disk in the same place where the old one was) while the application was running.

+4
source share
3 answers

You can run your code with the -verbose:class flag, and you will see that loading the class is extremely lazy. The JVM only loads classes, which it must necessarily continue. If at the end of your program you do something that requires a class that was previously not absolutely necessary, it will be loaded just then.

This very well explains the error you received, and you should note that you should not replace the jar file (or class) that works, for this very reason.

+2
source

No, the JVM reads from the jar every time it needs to load a new class.

+2
source

If you use import and normal use of classes than just once for each class.

If you play with class loaders, reflections, than you can do extra readings and "unplanned behavior" - this means many times in the class.

+1
source

All Articles