Does the Java JVM load an entire jar or ear if it only uses one class from it?

Jar files are a form of zip files.

How these are handled is highly dependent upon JRE.

Old versions of the Sun JRE used to memory map the entire file. This would allocate logical memory, but not necessarily cause any of the data to be loaded from disk. (32-bit Windows is not generally capable of allocating 3 GB of contiguous memory, although you can do it on other OSs).

I believe the current behaviour is to memory map the central directory at the rear of the file under Windows. Under other OSs, it's just read. This is controlled by #defines in the source.

JDK7 is likely to do something else.

Classes are generally loaded lazily. Resources are reread each time. java.util.ResourceBundle caches.


JVM loads only the required classes from the Jar when invoked. If application needs a class then the class and all other dependent classes will be loaded. Not sure but I suppose this is the responsibility of the class loader to locate the class from the classpath and load.

Tags:

Java

Class

Loader