An example of an in-depth understanding of ClassLoader

Source: Internet
Author: User

The file class loader, which loads the LoadClass method, logically reads only the file to load the class, not the parent ClassLoader to load

 PackageCom.ydd.study.hello.classloader;ImportJava.io.ByteArrayOutputStream;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.IOException;ImportJava.io.InputStream; Public classFilesystemclassloaderextendsClassLoader {PrivateString RootDir;  PublicFilesystemclassloader (String rootdir) { This. RootDir =RootDir; }     protectedClass<?> Findclass (String name)throwsClassNotFoundException {byte[] Classdata =getclassdata (name); if(Classdata = =NULL) {             Throw Newclassnotfoundexception (); }         Else {             returnDefineClass (name, Classdata, 0, classdata.length); }} @Override PublicClass<?>loadclass (String name) {Try {            returnFindclass (name); } Catch(ClassNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); return NULL; }                    }    Private byte[] Getclassdata (String className) {string path=Classnametopath (className); Try{InputStream ins=NewFileInputStream (path); Bytearrayoutputstream BAOs=NewBytearrayoutputstream (); intBufferSize = 4096; byte[] buffer =New byte[buffersize]; intBytesnumread = 0;  while((Bytesnumread = ins.read (buffer))! =-1) {baos.write (buffer,0, Bytesnumread); }             returnBaos.tobytearray (); } Catch(IOException e) {e.printstacktrace (); }         return NULL; }     Privatestring Classnametopath (String className) {returnRootDir +File.separatorchar+ classname.replace ('. ', File.separatorchar) + ". Class"; } }

Test code

 PackageCom.ydd.study.hello.classloader;Importorg.junit.Test; Public classfilesystemclassloadertest {@Test Public voidTest () {Filesystemclassloader Filesystemclassloader=NewFilesystemclassloader ("d:\\"); Try{Class<?>c=filesystemclassloader.loadclass ("TestClass");            System.out.println (c); Object TestClass=(C.newinstance ()); System.out.println (c+"|" +TestClass); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); }            }}

From the output below, you can see:

1. When loading the TestClass class, it has already caused other classes to load.

2, do not easily overload loadclass (), parental system is very important, otherwise it will cause a class to exist multiple class objects.

3, Class A depends on the loading of other classes, all of the classloader of Class A to initiate the initial loading

Java.io.filenotfoundexception:d:\java\lang\object.class (the system cannot find the path specified.) ) at Java.io.FileInputStream.open (Native Method) at java.io.fileinputstream.<init> (fileinputstream.java:146) At Java.io.fileinputstream.<init> (fileinputstream.java:101) at Com.ydd.study.hello.classloader.FileSystemClassLoader.getClassData (filesystemclassloader.java:42) at Com.ydd.study.hello.classloader.FileSystemClassLoader.findClass (filesystemclassloader.java:17) at Com.ydd.study.hello.classloader.FileSystemClassLoader.loadClass (filesystemclassloader.java:29) at Java.lang.ClassLoader.defineClass1 (Native Method) at Java.lang.ClassLoader.defineClass (classloader.java:800) at Java.lang.ClassLoader.defineClass (classloader.java:643) at Com.ydd.study.hello.classloader.FileSystemClassLoader.findClass (filesystemclassloader.java:22) at Com.ydd.study.hello.classloader.FileSystemClassLoader.loadClass (filesystemclassloader.java:29) at Com.ydd.study.hello.classloader.FileSystemClassLoaderTest.test (filesystemclassloadertest.java:12) at Sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method) at Sun.reflect.NativeMethodAccessorImpl.invoke ( nativemethodaccessorimpl.java:57) at Sun.reflect.DelegatingMethodAccessorImpl.invoke ( delegatingmethodaccessorimpl.java:43) at Java.lang.reflect.Method.invoke (method.java:606) at Org.junit.runners.model.frameworkmethod$1.runreflectivecall (frameworkmethod.java:47) at Org.junit.internal.runners.model.ReflectiveCallable.run (Reflectivecallable.java:12) at Org.junit.runners.model.FrameworkMethod.invokeExplosively (frameworkmethod.java:44) at Org.junit.internal.runners.statements.InvokeMethod.evaluate (invokemethod.java:17) at Org.junit.runners.ParentRunner.runLeaf (parentrunner.java:271) at Org.junit.runners.BlockJUnit4ClassRunner.runChild (blockjunit4classrunner.java:70) at Org.junit.runners.BlockJUnit4ClassRunner.runChild (BLOCKJUNIT4CLASSRUNNER.JAVA:50) at Org.junit.runners.parentrunner$3.run (parentrunner.java:238) at Org.junit.runners.parentrunner$1.schedule ( parentrunner.java:63) at org.junit.ruNners. Parentrunner.runchildren (parentrunner.java:236) at org.junit.runners.parentrunner.access$000 (ParentRunner.java : @ org.junit.runners.parentrunner$2.evaluate (parentrunner.java:229) at Org.junit.runners.ParentRunner.run ( parentrunner.java:309) at Org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run ( JUNIT4TESTREFERENCE.JAVA:50) at Org.eclipse.jdt.internal.junit.runner.TestExecution.run (testexecution.java:38) at Org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests (remotetestrunner.java:459) at Org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests (remotetestrunner.java:675) at Org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run (remotetestrunner.java:382) at Org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main (remotetestrunner.java:192) Java.lang.ClassNotFoundExceptionat Com.ydd.study.hello.classloader.FileSystemClassLoader.findClass ( filesystemclassloader.java:19) at Com.ydd.study.hello.classloader.FileSystemClassLoader.loadClass ( Filesystemclassloader.java:29) at Java.lang.ClassLoader.defineClass1 (Native Method) at Java.lang.ClassLoader.defineClass ( classloader.java:800) at Java.lang.ClassLoader.defineClass (classloader.java:643) at Com.ydd.study.hello.classloader.FileSystemClassLoader.findClass (filesystemclassloader.java:22) at Com.ydd.study.hello.classloader.FileSystemClassLoader.loadClass (filesystemclassloader.java:29) at Com.ydd.study.hello.classloader.FileSystemClassLoaderTest.test (Filesystemclassloadertest.java:12) at SUN.REFLECT.NATIVEMETHODACCESSORIMPL.INVOKE0 (Native Method) at Sun.reflect.NativeMethodAccessorImpl.invoke ( nativemethodaccessorimpl.java:57) at Sun.reflect.DelegatingMethodAccessorImpl.invoke ( delegatingmethodaccessorimpl.java:43) at Java.lang.reflect.Method.invoke (method.java:606) at Org.junit.runners.model.frameworkmethod$1.runreflectivecall (frameworkmethod.java:47) at Org.junit.internal.runners.model.ReflectiveCallable.run (Reflectivecallable.java:12) at Org.junit.runners.model.FrameworkMethod.invokeExplosively (Frameworkmethod.java:44) at Org.junit.internal.runners.statements.InvokeMethod.evaluate (invokemethod.java:17) at Org.junit.runners.ParentRunner.runLeaf (parentrunner.java:271) at Org.junit.runners.BlockJUnit4ClassRunner.runChild (blockjunit4classrunner.java:70) at Org.junit.runners.BlockJUnit4ClassRunner.runChild (BLOCKJUNIT4CLASSRUNNER.JAVA:50) at Org.junit.runners.parentrunner$3.run (parentrunner.java:238) at Org.junit.runners.parentrunner$1.schedule ( parentrunner.java:63) at Org.junit.runners.ParentRunner.runChildren (parentrunner.java:236) at org.junit.runners.parentrunner.access$000 (parentrunner.java:53) at Org.junit.runners.parentrunner$2.evaluate ( parentrunner.java:229) at Org.junit.runners.ParentRunner.run (parentrunner.java:309) at Org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run (JUNIT4TESTREFERENCE.JAVA:50) at Org.eclipse.jdt.internal.junit.runner.TestExecution.run (testexecution.java:38) at Org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests (remotetestrunner.java:459) at ORG.ECLIPSE.JDt.internal.junit.runner.RemoteTestRunner.runTests (remotetestrunner.java:675) at Org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run (remotetestrunner.java:382) at Org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main (remotetestrunner.java:192)

  

An example of an in-depth understanding of ClassLoader

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.