How JVM starts looking for classes?
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.
Without using any additional classloader:
- Search order for a JVM:
- Runtime classes (basically,
rt.jar
in$JRE_HOME/lib
`) - Extension classes (some JARs in
$JRE_HOME/lib/ext
`) - Classpath, in order. There are four possibilities for specifying classpath:
- If
-jar
was specified, then that JAR is in the classpath. Whatever classpath is declared as classpath inMETA-INF/MANIFEST.MF
is also considered. - Else, if
-cp
was specified, that is the classpath. - Else, if
$CLASSPATH
is set, that is the classpath. - Else, the current directory from which
java
has been launched is the classpath.
-cp src/A.jar:src/B.jar
, thenA.jar
will 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
-cp
or$CLASSPATH
. - In single thread scenarios and with recent JVMs,
Vector
andArrayList
should have similar performance (ArrayList
should perform slightly better as it is notsynchronized
, 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.