Reputation: 681
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?
Upvotes: 13
Views: 7476
Reputation: 11
Upvotes: 1
Reputation: 19712
There are two concepts involved
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 Class
object 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.
Upvotes: 5
Reputation: 68725
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.
Upvotes: 15
Reputation: 3146
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.
Upvotes: 0
Reputation: 1481
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
Upvotes: 0