classloader - In Java, is it possible to know whether a class has already been loaded? -


Is it possible to know that the Java class has been loaded, without to load it Effort? class.forName tries to load the square, but I do not want this side effect.

(I do not want to override the class loader. I am looking for a relatively simple method.)

(thanks to Alexei) This code:

  public class test loaded {public static zero main (string [] args throws exceptions {Java .lang .reflect.Method m = ClassLoader.class.getDeclaredMethod ("findLoadedClass", new class [] {String.class}); M.setAccessible (true); Class loader 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); } Fixed class ClassToTest {static {System.out.println ("Loading" + ClassToTest.class.getName ()); } Fixed zero reports loaded () {System.out.println ("loaded"); }}}  

Production:

  false loading Loading TestLoaded $ ClassToTest Loaded Truth  < / Pre> 

Note that the example class packages are not in full is required.

An example of a binary name is "java.security.KeyStore $ Builder $ FileBuilder $ 1"


Comments