标签:类型 read child 问题: within parent more ted 没有
1 #### NoClassDefFoundErrorSimulator.java 2 package org.ph.javaee.training3; 3 4 import java.net.URL; 5 import java.net.URLClassLoader; 6 7 import org.ph.javaee.training.util.JavaEETrainingUtil; 8 9 /** 10 * NoClassDefFoundErrorSimulator 11 * @author Pierre-Hugues Charbonneau 12 * 13 */ 14 public class NoClassDefFoundErrorSimulator { 15 16 /** 17 * @param args 18 */ 19 public static void main(String[] args) { 20 21 System.out.println("java.lang.NoClassDefFoundError Simulator - Training 3"); 22 System.out.println("Author: Pierre-Hugues Charbonneau"); 23 System.out.println("http://javaeesupportpatterns.blogspot.com"); 24 25 // Local variables 26 String currentThreadName = Thread.currentThread().getName(); 27 String callerFullClassName = "org.ph.javaee.training3.CallerClassA"; 28 29 // Print current ClassLoader context & Thread 30 System.out.println("\nCurrent Thread name: ‘"+currentThreadName+"‘"); 31 System.out.println("Initial ClassLoader chain: "+JavaEETrainingUtil.getCurrentClassloaderDetail()); 32 33 try { 34 // Location of the application code for our child ClassLoader 35 URL[] webAppLibURL = new URL[] {new URL("file:caller.jar"),new URL("file:referencer.jar")}; 36 37 // Child ClassLoader instance creation 38 URLClassLoader childClassLoader = new URLClassLoader(webAppLibURL); 39 40 /*** Application code execution... ***/ 41 42 // 1. Change the current Thread ClassLoader to the child ClassLoader 43 Thread.currentThread().setContextClassLoader(childClassLoader); 44 System.out.println(">> Thread ‘"+currentThreadName+"‘ Context ClassLoader now changed to ‘"+childClassLoader+"‘"); 45 System.out.println("\nNew ClassLoader chain: "+JavaEETrainingUtil.getCurrentClassloaderDetail()); 46 47 // 2. Load the caller Class within the child ClassLoader... 48 System.out.println(">> Loading ‘"+callerFullClassName+"‘ to child ClassLoader ‘"+childClassLoader+"‘..."); 49 Class<?> callerClass = childClassLoader.loadClass(callerFullClassName); 50 51 // 3. Create a new instance of CallerClassA 52 Object callerClassInstance = callerClass.newInstance(); 53 54 } catch (Throwable any) { 55 System.out.println("Throwable: "+any); 56 any.printStackTrace(); 57 } 58 59 System.out.println("\nSimulator completed!"); 60 } 61 }
1 #### CallerClassA.java 2 package org.ph.javaee.training3; 3 4 import org.ph.javaee.training3.ReferencingClassA; 5 6 /** 7 * CallerClassA 8 * @author Pierre-Hugues Charbonneau 9 * 10 */ 11 public class CallerClassA { 12 13 private final static Class<CallerClassA> CLAZZ = CallerClassA.class; 14 15 static { 16 System.out.println("Class loading of "+CLAZZ+" from ClassLoader ‘"+CLAZZ.getClassLoader()+"‘ in progress..."); 17 } 18 19 public CallerClassA() { 20 System.out.println("Creating a new instance of "+CallerClassA.class.getName()+"..."); 21 22 doSomething(); 23 } 24 25 private void doSomething() { 26 27 // Create a new instance of ReferencingClassA 28 ReferencingClassA referencingClass = new ReferencingClassA(); 29 } 30 }
1 #### ReferencingClassA.java 2 package org.ph.javaee.training3; 3 4 /** 5 * ReferencingClassA 6 * @author Pierre-Hugues Charbonneau 7 * 8 */ 9 public class ReferencingClassA { 10 11 private final static Class<ReferencingClassA> CLAZZ = ReferencingClassA.class; 12 13 static { 14 System.out.println("Class loading of "+CLAZZ+" from ClassLoader ‘"+CLAZZ.getClassLoader()+"‘ in progress..."); 15 } 16 17 public ReferencingClassA() { 18 System.out.println("Creating a new instance of "+ReferencingClassA.class.getName()+"..."); 19 } 20 21 public void doSomething() { 22 //nothing to do... 23 } 24 }
1 ## Baseline (normal execution) 2 <JDK_HOME>\bin>java -classpath MainProgram.jar org.ph.javaee.training3.NoClassDefFoundErrorSimulator 3 4 java.lang.NoClassDefFoundError Simulator - Training 3 5 Author: Pierre-Hugues Charbonneau 6 http://javaeesupportpatterns.blogspot.com 7 8 Current Thread name: ‘main‘ 9 Initial ClassLoader chain: 10 ----------------------------------------------------------------- 11 sun.misc.Launcher$ExtClassLoader@17c1e333 12 --- delegation --- 13 sun.misc.Launcher$AppClassLoader@214c4ac9 **Current Thread ‘main‘ Context ClassLoader** 14 ----------------------------------------------------------------- 15 16 >> Thread ‘main‘ Context ClassLoader now changed to ‘java.net.URLClassLoader@6a4d37e5‘ 17 18 New ClassLoader chain: 19 ----------------------------------------------------------------- 20 sun.misc.Launcher$ExtClassLoader@17c1e333 21 --- delegation --- 22 sun.misc.Launcher$AppClassLoader@214c4ac9 23 --- delegation --- 24 java.net.URLClassLoader@6a4d37e5 **Current Thread ‘main‘ Context ClassLoader** 25 ----------------------------------------------------------------- 26 27 >> Loading ‘org.ph.javaee.training3.CallerClassA‘ to child ClassLoader ‘java.net.URLClassLoader@6a4d37e5‘... 28 Class loading of class org.ph.javaee.training3.CallerClassA from ClassLoader ‘java.net.URLClassLoader@6a4d37e5‘ in progress... 29 Creating a new instance of org.ph.javaee.training3.CallerClassA... 30 Class loading of class org.ph.javaee.training3.ReferencingClassA from ClassLoader ‘java.net.URLClassLoader@6a4d37e5‘ in progress... 31 Creating a new instance of org.ph.javaee.training3.ReferencingClassA... 32 33 Simulator completed!
1 ## Problem reproduction run (static variable initializer failure) 2 <JDK_HOME>\bin>java -classpath MainProgram.jar;caller.jar org.ph.javaee.training3.NoClassDefFoundErrorSimulator 3 4 java.lang.NoClassDefFoundError Simulator - Training 3 5 Author: Pierre-Hugues Charbonneau 6 http://javaeesupportpatterns.blogspot.com 7 8 Current Thread name: ‘main‘ 9 Initial ClassLoader chain: 10 ----------------------------------------------------------------- 11 sun.misc.Launcher$ExtClassLoader@17c1e333 12 --- delegation --- 13 sun.misc.Launcher$AppClassLoader@214c4ac9 **Current Thread ‘main‘ Context ClassLoader** 14 ----------------------------------------------------------------- 15 16 >> Thread ‘main‘ Context ClassLoader now changed to ‘java.net.URLClassLoader@6a4d37e5‘ 17 18 New ClassLoader chain: 19 ----------------------------------------------------------------- 20 sun.misc.Launcher$ExtClassLoader@17c1e333 21 --- delegation --- 22 sun.misc.Launcher$AppClassLoader@214c4ac9 23 --- delegation --- 24 java.net.URLClassLoader@6a4d37e5 **Current Thread ‘main‘ Context ClassLoader** 25 ----------------------------------------------------------------- 26 27 >> Loading ‘org.ph.javaee.training3.CallerClassA‘ to child ClassLoader ‘java.net.URLClassLoader@6a4d37e5‘... 28 Class loading of class org.ph.javaee.training3.CallerClassA from ClassLoader ‘sun.misc.Launcher$AppClassLoader@214c4ac9‘ in progress...// Caller is loaded from the parent class loader, why??? 29 Creating a new instance of org.ph.javaee.training3.CallerClassA... 30 Throwable: java.lang.NoClassDefFoundError: org/ph/javaee/training3/ReferencingClassA 31 java.lang.NoClassDefFoundError: org/ph/javaee/training3/ReferencingClassA 32 at org.ph.javaee.training3.CallerClassA.doSomething(CallerClassA.java:27) 33 at org.ph.javaee.training3.CallerClassA.<init>(CallerClassA.java:21) 34 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 35 at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) 36 at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) 37 at java.lang.reflect.Constructor.newInstance(Unknown Source) 38 at java.lang.Class.newInstance0(Unknown Source) 39 at java.lang.Class.newInstance(Unknown Source) 40 at org.ph.javaee.training3.NoClassDefFoundErrorSimulator.main(NoClassDefFoundErrorSimulator.java:51) 41 Caused by: java.lang.ClassNotFoundException: org.ph.javaee.training3.ReferencingClassA 42 at java.net.URLClassLoader$1.run(Unknown Source) 43 at java.net.URLClassLoader$1.run(Unknown Source) 44 at java.security.AccessController.doPrivileged(Native Method) 45 at java.net.URLClassLoader.findClass(Unknown Source) 46 at java.lang.ClassLoader.loadClass(Unknown Source) 47 at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) 48 at java.lang.ClassLoader.loadClass(Unknown Source) 49 ... 9 more 50 51 Simulator completed!
翻译自:https://javaeesupportpatterns.blogspot.com/2012/08/javalangnoclassdeffounderror-parent.html
标签:类型 read child 问题: within parent more ted 没有
原文地址:https://www.cnblogs.com/029zz010buct/p/10366808.html