Search code examples
javajvmclassloader

How JVM starts looking for classes?


  • 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?


Solution

  • 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.