(Thanks to Aleksi) This code:
public class TestLoaded {
public static void main(String[] args) throws Exception {
java.lang.reflect.Method m = ClassLoader.class.getDeclaredMethod("findLoadedClass", new Class[] { String.class });
m.setAccessible(true);
ClassLoader cl = ClassLoader.getSystemClassLoader();
Object test1 = m.invoke(cl, "TestLoaded$ClassToTest");
System.out.println(test1 != null);
ClassToTest.reportLoaded();
Object test2 = m.invoke(cl, "TestLoaded$ClassToTest");
System.out.println(test2 != null);
}
static class ClassToTest {
static {
System.out.println("Loading " + ClassToTest.class.getName());
}
static void reportLoaded() {
System.out.println("Loaded");
}
}
}
Produces:
false
Loading TestLoaded$ClassToTest
Loaded
true
Note that the example classes are not in a package. The full binary name is required.
An example of a binary name is "java.security.KeyStore$Builder$FileBuilder$1"
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…