Java notes classloader Getresourceasstream() and class The difference between getresourceasstream()


Class. Getresourceasstream() will specify that the resource path to be loaded is consistent with the path of the package where the current class is located.

For example, you wrote a mytest class in the package com test. Under mycode, then mytest class.getResourceAsStream("name")Will be on COM test. Find the corresponding resources under the mycode package.

If the name starts with ‘/’, it will be searched from the root path of the classpath.

ClassLoader. Getresourceasstream() will search from the root path of the classpath regardless of whether the resource to be searched is preceded by ‘/’.

So: mytest getClassLoader(). Getresourceasstream (“name”) and

MyTest. getClassLoader(). The effect of getresourceasstream (“name”) is the same.

By the way, the class loader in Java:

There are three kinds of loaders

Bootstrap classloader: it is responsible for loading Java core classes (contents in lib and class directories under JRE)
Extension classloader: responsible for loading Java extension classes (contents in lib / ext directory under JRE)
System classloader: it is responsible for loading the class specified by the application (the content configured in the environment variable classpath)

The loading order of a class is also arranged according to the above, so as to ensure that the classes of the system can be loaded first.

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

This involves class Getclassloader () and thread currentThread. The difference between getcontextclassloader().

Take a simple example:

One day, Java provides us with a class called startcamera to start the standard camera of the computer, and packages this class in a jar.

Normally, when we want to start the camera, we only need to configure this jar into the classpath. When the system starts, the system classloader will load this class into the application.

However, because the manufacturers of cameras are different, there will be many different startcamera implementations for new devices. We don’t know which one the actual users will use in the application. So we customized a classloader to load the corresponding startamera class for specific device types.

In this way, the class defined by us will be loaded first, and the system will be loaded if it cannot be loaded. Such requirements cannot be met by the default parent delegate loading mechanism of the system.

Thread. currentThread. This is how getcontextclassloader () is generated. We use thread currentThread. SetContextClassLoader () can specify the corresponding classloader for the current thread, and then get it in the way of get.

Then the above loading code may look like this:





Here is a note about Java classloader Getresourceasstream() and class This is the difference between getresourceasstream () and Java classloader Getresourceasstream() and class Getresourceasstream() different content, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you can support developeppaer in the future!

Recommended Today

Vue2 technology finishing 3 – Advanced chapter – update completed

3. Advanced chapter preface Links to basic chapters: Link to component development: 3.1. Custom events of components 3.1.1. Binding custom events There are two implementation methods here: one is to use v-on with vuecomponent$ Emit implementation [PS: this method is a little similar to passing from child to parent]; The other is to use ref […]