问题
I was curious about what all locations JVM looks for executing a program? I'm more interested in understanding in what sequence and where does JVM look for class files, like does it look into java libs, extension libs, classpath any directory like the current directory from where the java is invoked? I'm more interested in JVM behaviour and not how class loader load class, which I know has parent delegation mechanism till root.
If a class is executed from directory where the compiled class is kept on file system and also in a jar file in the same directory, would JVM load both or just one and which one?
Say you have a thread unsafe
Vectorand if we compare it performance toArrayList, which one would be better and why?
回答1:
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:
- classes or packages in current directory.
- classes found from CLASSPATH environment variable. [overrides 1]
- classes found from -classpath command line option. [overrides 1,2]
- 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.
回答2:
Without using any additional classloader:
- Search order for a JVM:
- Runtime classes (basically,
rt.jarin$JRE_HOME/lib`) - Extension classes (some JARs in
$JRE_HOME/lib/ext`) - Classpath, in order. There are four possibilities for specifying classpath:
- If
-jarwas specified, then that JAR is in the classpath. Whatever classpath is declared as classpath inMETA-INF/MANIFEST.MFis also considered. - Else, if
-cpwas specified, that is the classpath. - Else, if
$CLASSPATHis set, that is the classpath. - Else, the current directory from which
javahas been launched is the classpath.
-cp src/A.jar:src/B.jar, thenA.jarwill be searched first, thenB.jar - If
- Runtime classes (basically,
- 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
-cpor$CLASSPATH. - In single thread scenarios and with recent JVMs,
VectorandArrayListshould have similar performance (ArrayListshould perform slightly better as it is notsynchronized, but locking is fast currently when there is no contention, so the difference should be small). Anyway,Vectoris obsolete: don't use it in new code.
回答3:
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.
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.
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
回答4:
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?
回答5:
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.
来源:https://stackoverflow.com/questions/8934780/how-jvm-starts-looking-for-classes