13

When we invoke java command with -cp command then we provide some directories and jar files. Does jvm load all the classes mentioned by the classpath Or it is just a super set of all classes which jvm will look up to load when required?

hsingh
  • 661
  • 5
  • 26

5 Answers5

14

Does jvm load all the classes mentioned by the classpath Or it is just a super set of all classes which jvm will look up to load when required?

JVM loads classes form the classpath on the need basis i.e. when a reference is found for the class, it is loaded. Also there is a hierarchy of class loaders in JVM, a class loaded by the parent class loader is used by the lower class loaders.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • I have written a simple HelloWorld class which does not import or use any other 3rd party jar. When I run this with "java -verbose:class HelloWorld" then I see that so many classes from rt.jar is loaded by the JVM. Still confused for core java classes loaded by Bootstrap loader. Are all classes loaded from rt.jar? – hsingh Jun 25 '15 at 03:59
  • @hsingh JVM requires classes present in rt.jar to setup java runtime environment. I am not sure whether all the classes but surely some are loaded from rt.jar – Juned Ahsan Jun 25 '15 at 04:22
5

There are two concepts involved

  • loading
  • initialization

Initializing a class will initialize fields and execute static blocks. The exact moment this happens is important to application semantics, therefore it is precisely defined.

Initialization requires loading first; but loading is more of an internal concept of the JVM. JVM could, and is allowed to, aggressively preload classes even if unneeded. This process does not affect the application semantics and it is invisible to the application.

As far as the application is concerned, a class must have been loaded if we get a Classobject of it, e.g. from Foo.class, Class.forName, or other reflection APIs. We can examine the properties of the Class without necessarily triggering initialization.

One important constraint - we must get the same Class object for the same class name (and from the same classloader). The Class object is the representation of the loaded class.

ZhongYu
  • 19,446
  • 5
  • 33
  • 61
  • Concerning your last point, I don't think the `Class` object **needs** to be the same. I think it's just a good practice for implementations. Need to find that in JLS. – Sotirios Delimanolis Aug 12 '15 at 02:15
  • Yeah, [_Given the same name, a good class loader should always return the same class object._](http://docs.oracle.com/javase/specs/jls/se8/html/jls-12.html#jls-12.2) Unless I'm misinterpreting. – Sotirios Delimanolis Aug 12 '15 at 02:16
  • 2
    @SotiriosDelimanolis - a non-good class loader could even return a wrong Class object! I think the sentence means a "legit" class loader should behave that way. There are plenty of mentioning of "*the* Class object" in JLS and javadocs, e.g. `static synchronized` operates on "the Class object"; it won't make much sense if there could be 2 Class objects for a class. Besides, `Class` does not override `equals`, and there are plenty of code using `==` directly for checking Class equality, even in examples in JLS. – ZhongYu Aug 12 '15 at 02:53
1
  1. JVM loads only the referenced classes and not every class present in the jars in your classpath
  2. Loading of classes happens through a hierarchical manner
  3. Having too many jars in the classpath only occupies more disk space
  4. JVM only uses memory in your physical memory(RAM).
0

Loading of class occurs in order and looks in locations. -cp falls under third category as listed below .Mostly the application classes should be supplied through -cp or it will look for the enviroment variable CLASSPATH.

The extension framework makes use of the class-loading delegation mechanism. When the runtime environment needs to load a new class for an application, it looks for the class in the following locations, in order:

1)Bootstrap classes: the runtime classes in rt.jar, internationalization classes in i18n.jar, and others.

2)Installed extensions: classes in JAR files in the lib/ext directory of the JRE, and in the system-wide, platform-specific extension directory (such as /usr/jdk/packages/lib/ext on the Solaris™ Operating System, but note that use of this directory applies only to Java™ 6 and later).

3)The class path: classes, including classes in JAR files, on paths specified by the system property java.class.path. If a JAR file on the class path has a manifest with the Class-Path attribute, JAR files specified by the Class-Path attribute will be searched also. By default, the java.class.path property's value is ., the current directory. You can change the value by using the -classpath or -cp command-line options, or setting the CLASSPATH environment variable. The command-line options override the setting of the CLASSPATH environment variable.

https://docs.oracle.com/javase/tutorial/ext/basics/load.html

KDP
  • 1,481
  • 7
  • 13
  • 1
    So `-cp` causes the classes to be loaded automatically? – Chetan Kinger Jun 24 '15 at 12:56
  • -cp is the classpath we show to the runtime environment the path where it can find the required classes which are not in first 2 locations (rt.jar and extensions). if the required class is not found in any of the three locations will end up in classnotfound exception – KDP Jun 24 '15 at 12:59
  • The question is not how `-cp` works or how classes are looked up. The question is whether classes/jars listed with `-cp` get loaded automatically. Your answer explains the order in which classes are searched. That is not what the OP asked. The only part of your answer that talks about loading is *Loading of class occurs in order* which doesn't answer the question. – Chetan Kinger Jun 24 '15 at 13:01
  • it looks for the class files in the mentioned locations in order. it doesn;t mean it will load all classes..it will look in the mentioned locations for the class file .your classpath can contain n classes but the required class will be loaded.Hope this makes it clear. – KDP Jun 24 '15 at 13:08
  • That's right. What I am saying is that your answer needs to explicitly mention that *the required class will be loaded when referenced* – Chetan Kinger Jun 24 '15 at 13:30
0

It doesn't load all the classes, but it knows where to look for them when you need them.

They will be loaded when they are first needed.

Phil Anderson
  • 3,146
  • 13
  • 24