The difference between JAVA note Classloader.getresourceasstream () and Class.getresourceasstream ()

Source: Internet
Author: User

   Class.getResourceAsStream() 会指定要加载的资源路径与当前类所在包的路径一致。

For example you wrote a MyTest class under package com.test.mycode, then Mytest.class. getResourceAsStream("name") The appropriate resources will be found under the Com.test.mycode package.

If this name starts with a '/', then it is searched from the root path of the Classpath.

Classloader.getresourceasstream () will look up from the root path of classpath regardless of whether the resource you are looking for is preceded with a '/'.

So: mytest.getclassloader (). getResourceAsStream ("name") and

The effect of Mytest.getclassloader (). getResourceAsStream ("name") is the Same.

By the way, the loader for the class in Java:

A total of three loaders

Bootstrap classloader: responsible for loading Java core Classes (content in the JRE Lib and class Directories) extension classloader: responsible for loading Java extension Classes (content in the Lib/ext directory under Jre) system Classloader: responsible for loading application-specified classes (content configured in environment variables Classpath)

The loading order of a class is also arranged as above, so that the class of the system can be loaded First.

At the same time, users can define their own classloader to load special Resources.

This involves the difference between Class.getclassloader () and Thread.currentThread.getContextClassLoader ().

To give a simple example:

If one day Java gives us a class called Startcamera to start the Computer's standard camera, and the class is packaged in a jar.

normally, We just need to configure this jar to Classpath when we start the Camera. System ClassLoader will load this class into the app when it is Started.

But because the camera manufacturer is not the same, for the new device there will be a number of different Startcamera implementations, in the application we do not know what the actual user will Use. We then customized a classloader to load the corresponding Startcamera class for the specific device type.

This makes it possible to load the system without loading the class that we defined First. The requirement is that the system default parent delegate loading mechanism cannot be satisfied.

Thread.currentThread.getContextClassLoader () is produced in this way. We use Thread.currentThread.setContextClassLoader () to specify the appropriate classloader for the current thread and get it in a get way.

 public void usecamera () {  this. findclassloader (). loadclass ("startcamera");  S.start ();} Private ClassLoader findclassloader () {  = thread.currentthread (). getcontextclassloader ();   if (loader==null) {     = classloader.getsystemclassloader ();  }   return loader;}

The difference between JAVA note Classloader.getresourceasstream () and Class.getresourceasstream ()

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.