简体   繁体   中英

ClassLoader does not load inner class

ClassLoader clMain -> URLClassLoader clA (class A, $B). $B is a inner class B of the class A.

  1. From clMain creating new clA with specified url to the jar file which contains class A (and class $B). Then creating new object A via clA.
  2. Another class, loaded from X ClassLoader , execute a method of the object A via interface without problems.
  3. Then, in that method, should be created new object $B, but it throws the NoClassDefFoundError .
  4. Loading the class $B in the constructor of the class A is solving the problem: this.getClass().getClassLoader().loadClass("A$B");

So, the cuestion is: "Why the definition of the class $B cannot be found for step 3 and Can be solved this porblem in another way?"

Checking the ClassLoader used in the method ( this.getClass().getClassLoader() ) and comparing it with the ClassLoader used in the constructor - is the same and contains the necessary url. Java 8 and 14 tried to run, jdk 8 to build.

NoClassDefFoundError invariably means the $inner class file (in this specific case) itself could not be found . packaging or copying problem or a url referencing problem (? JNDI naming problem) but whatever, the file was not located where stated. (sometimes the filename is a typo somewhere and the location to it is correct).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM