Java7 bootstrap: Checking class without loading?

和自甴很熟 提交于 2019-12-06 04:17:20

It is possible to load a class without linking it: the launcher in Java 8 loads the main class by calling the loadClass method on the classloader, which does not link the class or run any of its static methods.

See http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/sun/launcher/LauncherHelper.java#l495 and java.c#l1221 for the code that loads the main class in Java 8.

The Java 6 launcher uses the FindClass JNI method to load the main class: I'm guessing that this accounts for the difference in behaviour. See jdk6/jdk6/jdk/file/814bf0775b52/src/share/bin/java.c#l1387

FindClass does initialize the class, at least on Java 6: the implementation in jdk6/jdk6/hotspot/file/91e5cc5d33b9/src/share/vm/prims/jni.cpp

result = find_class_from_class_loader(env, sym, true, loader, protection_domain, true, thread);

calls, in jdk6/jdk6/hotspot/file/91e5cc5d33b9/src/share/vm/prims/jvm_misc.hpp,

jclass find_class_from_class_loader(JNIEnv* env, Symbol* name, jboolean init, Handle loader, Handle protection_domain, jboolean throwError, TRAPS);

with init as true

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!