Java note classloader. getresourceasstream () and Class. getresourceasstream ()

Source: Internet
Author: User
Class. getresourceasstream () specifies the path of the resource to be loaded to be consistent with the path of the package of the current class.

For example, if you write a mytest class under the com. Test. mycode package, then mytest. Class.Getresourceasstream ("name ")Find the corresponding resource under the com. Test. mycode package.

If the name starts with '/', it will start from the root path of classpath.

Classloader. getresourceasstream () is located in the root path of classpath no matter whether '/' is included in the resource to be searched.

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

The effects of mytest. getclassloader (). getresourceasstream ("name") are the same.

 

By the way, the class loaders in Java:

There are three loaders in total

Bootstrap classloader: responsible for loading Java core classes (Content in lib and class directories under JRE)
Extension classloader: responsible for loading Java extension classes (content in the LIB/EXT directory under JRE)
System classloader: responsible for loading the specified class of the application (content configured in the environment variable classpath)

The loading sequence of a class is also arranged in the above order, so that the system class can be loaded first.

You can also define classloader to load special resources.

The difference between class. getclassloader () and thread. currentthread. getcontextclassloader () is involved.

Here is a simple example:

Suppose one day Java provided us with a class named startcamera to start the computer's standard camera and package this class in a jar.

Under normal circumstances, we only need to configure the jar to classpath when starting the camera. When the system starts, the system classloader loads the class into the application.

But because the camera manufacturers are different, there will be multiple different startcamera implementations for new devices. In the application, we do not know which kind of camera will be used by actual users. Therefore, we have customized a classloader to load the corresponding startcamera class for the specific device type.

In this way, the following occurs:First load the class we define and then load the system if it cannot be loaded.This is the default requirement of the system.Parent delegate Loading MechanismCannot meet.

 

Thread. currentthread. getcontextclassloader () is generated in this way. We can use thread. currentthread. setcontextclassloader () to specify the corresponding classloader for the current thread, and then get it by get.

Then the above loadingCodeIt may be like this:

Public Void Usecamera (){
Startcamera S = This . Findclassloader (). loadclass ("startcamera ");

S. Start ();
}

PrivateClassloader findclassloader (){
Classloader loader = thread. currentthread (). getcontextclassloader ();
If(Loader =Null){
Loader = classloader. getsystemclassloader ();
}

ReturnLoader;
}

 

 

 

 

 

 

 

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.