这一块还没有深入了解,不敢误人子弟。
我知道的,虚拟机在运行的时候,会预先加载一个常用的class,比如java.lang包下面的。
至于你在程序中自己引用的class文件/jar包之类的,是有一个加载过程的。
当虚拟机需要一个class文件,他会调用默认的classLoader.loadClass()去加载,这个方法首先在已经加载的class中查找,找得到当然返回,找不到则交给父加载器。层层查找没有之后,classLoader会调用findClass方法去文件中加载。
protected Class> loadClass(String name, boolean resolve)
throws ClassNotFoundException
{
synchronized (getClassLoadingLock(name)) {
// First, check if the class has already been loaded
Class> c = findLoadedClass(name);
if (c == null) {
long t0 = System.nanoTime();
try {
if (parent != null) {
c = parent.loadClass(name, false);
} else {
c = findBootstrapClassOrNull(name);
}
} catch (ClassNotFoundException e) {
// ClassNotFoundException thrown if class not found
// from the non-null parent class loader
}
if (c == null) {
// If still not found, then invoke findClass in order
// to find the class.
long t1 = System.nanoTime();
c = findClass(name);
// this is the defining class loader; record the stats
sun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0);
sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);
sun.misc.PerfCounter.getFindClasses().increment();
}
}
if (resolve) {
resolveClass(c);
}
return c;
}