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 Vector
and if we compare it performance to ArrayList
, which one would be better and why?
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:
So if you use -jar option while running, classes come from jarfile.
Only one class is loaded though.
Without using any additional classloader:
rt.jar
in $JRE_HOME/lib
`)$JRE_HOME/lib/ext
`)-jar
was specified, then that JAR is in the classpath. Whatever classpath is declared as classpath in META-INF/MANIFEST.MF
is also considered.-cp
was specified, that is the classpath.$CLASSPATH
is set, that is the classpath.java
has been launched is the classpath.-cp src/A.jar:src/B.jar
, then A.jar
will be searched first, then B.jar
-cp
or $CLASSPATH
.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With