How JVM starts looking for classes?

人盡茶涼 提交于 2019-12-02 22:31:08

How classes are found. Answer is here:

http://docs.oracle.com/javase/1.5.0/docs/tooldocs/findingclasses.html

Answer for point 2: Order of finding classes is as follows:

  1. classes or packages in current directory.
  2. classes found from CLASSPATH environment variable. [overrides 1]
  3. classes found from -classpath command line option. [overrides 1,2]
  4. classes found from jar archives specified via -jar command line option [overrides 1,2,3]

So if you use -jar option while running, classes come from jarfile.

Only one class is loaded though.

Without using any additional classloader:

  • Search order for a JVM:
    1. Runtime classes (basically, rt.jar in $JRE_HOME/lib`)
    2. Extension classes (some JARs in $JRE_HOME/lib/ext`)
    3. Classpath, in order. There are four possibilities for specifying classpath:
      1. If -jar was specified, then that JAR is in the classpath. Whatever classpath is declared as classpath in META-INF/MANIFEST.MF is also considered.
      2. Else, if -cp was specified, that is the classpath.
      3. Else, if $CLASSPATH is set, that is the classpath.
      4. Else, the current directory from which java has been launched is the classpath.
      So, if I specify -cp src/A.jar:src/B.jar, then A.jar will be searched first, then B.jar
  • The JVM loads only the class that is found first, according to the order in which the directories/JARs are declared in the classpath. This is important if you use -cp or $CLASSPATH.
  • In single thread scenarios and with recent JVMs, Vector and ArrayList should have similar performance (ArrayList should perform slightly better as it is not synchronized, but locking is fast currently when there is no contention, so the difference should be small). Anyway, Vector is obsolete: don't use it in new code.
  1. I believe Java looks in the current directory, then at the class path, per the "-cp" VM argument. You can put any combination of folders of classes (e.g. /project/bin/com/putable), specific class files (e.g. /project/bin/com/putable/MyClass.class), and JAR files (e.g. /project/lib/MyJar.jar) on the class path. Locations are separated by either a colon (Unix-based OSes) or semicolon (Windows-based OSes). So anything on the classpath is fair game for Java to look at when obtaining class definitions. With respect to sequence, classes are loaded lazily. So they only get loaded when your application first requires them. If your application doesn't require a certain class during the duration of its runtime, then that class will NEVER get loaded.

  2. If you don't put anything on the class path, I think Java will load from the class file and not the Jar. If you specify one or the other on the classpath, then that's the place Java will look for. If you put both on the classpath, Java's class-loading behavior is undefined and it could pick either, depending on the JVM implementation.

  3. Depends on what you want to do. Vectors are actually always thread safe, per the Java API, so if you don't require concurrent access, the ArrayList will be faster. Vectors and ArrayLists are both backed by arrays, but they increase capacity at different rates (Vector capacity doubles whenever the end is reached and more space is needed, but ArrayList increases by 50%). Depending on how often you have to grow or shrink, the answer will vary. Check out this link for more info:

http://www.javaworld.com/javaworld/javaqa/2001-06/03-qa-0622-vector.html

I'm more interested in JVM behaviour and not how class loader load class

Sorry, but this is nonsensical. Because the answer is that the JVM creates a class loader and let's this class loader load the classes. So, in order to understand the "JVM behaviour" you need to understand the class loader behaviour.

But maybe your question was: how does the JVM create the system class loader?

The accepted answer is already correct but there is a more detailed and updated official spec in How Classes are Found.

Some caveats as:

A class file has a subpath name that reflects the class's fully-qualified name. For example, if the class com.mypackage.MyClass is stored under /myclasses, then /myclasses must be in the user class path and the full path to the class file must be /myclasses/com/mypackage/MyClass.class. If the class is stored in an archive named myclasses.jar, then myclasses.jar must be in the user class path, and the class file must be stored in the archive as com/mypackage/MyClass.class.

And the priorities in How the Java Launcher Finds User Classes

  • The default value, ".", meaning that user class files are all the class files in the current directory (or under it, if in a package).
  • The value of the CLASSPATH environment variable, which overrides the default value.
  • The value of the -cp or -classpath command line option, which overrides both the default value and the CLASSPATH value.
  • The JAR archive specified by the -jar option, which overrides all other values. If this option is used, all user classes must come
    from the specified archive.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!