Why do I get an exception when I run the following code? I went to the source code and found that this class was generated by ASM. Isn't the class generated in this way in the method area?
public class Main {
public static void main(String[] args) throws ClassNotFoundException {
Class<? extends Runnable> runClass = test().getClass();
ClassLoader classLoader = runClass.getClassLoader();
System.out.println(classLoader.loadClass(Main.class.getName()));
System.out.println(classLoader.loadClass(runClass.getName()));
}
public static Runnable test() {
return () -> {};
}
}
results
class link.yxw.Main
Exception in thread "main" java.lang.ClassNotFoundException: link.yxw.Main$$Lambda$1/1324119927
at java.net.URLClassLoader.findClass(URLClassLoader.java:387)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
at link.yxw.Main.main(Main.java:10)