Java solution to the path problem when loading resource files

  • 2020-04-01 01:43:11
  • OfStack

There are two common ways to load resource files:

One, with ClassLoader, said here we have to mention the ClassLoader classification, Java built-in classloaders are mainly three,

The first is the root class loader, written in C++, which loads key Java classes such as java.lang.object and other runtime code into memory first. Responsible for loading: BootStrp-- > JRE/lib/rt. The jar

The second is an extended ExtClassLoader, written by a Java class, which loads some of the classes in the JRE into memory. Package load: ExtClassLoader-- > JRE/lib/ext / *. The jar

The third is the application class loader, or system class loader, which loads classes from the CLASSPATH into memory. Through this. GetSystemClassLoader () to get the application class loader;


In addition, class loader is not a vertically inherited parent-child relationship, but a composite relationship. When the class loader is instantiated, the instance of the parent class loader can be passed to the class loader as a construction parameter.

For more information about class loaders, you can do your own search.
 

After getting to the application class loader, is to get the resource file, call loader.getresource (path) can load the corresponding path under the resource file, can not start with '/', about the package can be the package as a common folder, with '/' partition each package.

URL url2 =  This getSystemClassLoader (.) getResource (" demo/names. Ser "); Is to get the names.ser serialization file in the demo package.

Two, with the need to load the current class of getResource method to load, in fact, this method is also called to load the class loader to get the resource file, but the parameters are different.

  (1) to obtain the files in the package of the class, the relative path can be used to directly access the resources in the package; Such as: not. Class. GetResource (" names. Ser "); Gets the resources in the package where Demo1's class file resides

  (2) to get the resource file outside the package must start with '/', such as URL URL = demo1.class.getresource ("/demo/names.ser"); Gets the names.ser file in the demo package


In fact, the second way is a packaging of the first way, are used to use the ClassLoader to load the resource file. Why do you say that? Take a look at the source of the Class Class:


public java.net.URL getResource(String name) {
         name = resolveName(name);
         ClassLoader cl = getClassLoader0();
         if (cl==null) {
             // A system class.
             return ClassLoader.getSystemResource(name);
         }
         return cl.getResource(name);
     }


private String resolveName(String name) {
         if (name == null) {
             return name;
         }
         if (!name.startsWith("/")) {
             Class c = this;
             while (c.isArray()) {
                 c = c.getComponentType();
             }
             String baseName = c.getName();
             int index = baseName.lastIndexOf('.');
             if (index != -1) {
                 name = baseName.substring(0, index).replace('.', '/')
                     +"/"+name;
             }
         } else {
             name = name.substring(1);
         }
         return name;
     }

GetResource according to incoming name value (that is, the form of relative path or absolute path), then we see through resolveName processing to call this c1 load, this load path is not in the form of '/' at the beginning of the relative path, it must be resolveName converts the path a pair of, look at resolveName method, first of all determine whether with '/' at the beginning, if with '/' at the beginning, is the relative path, or is the absolute path, Notice that the else block, it gets rid of the first character, and it does get rid of it and it fits the load path of the ClassLoader, and the if block gets rid of it by intercepting the package path of the current class, and replacing the. With '/', and adding that relative path, it also forms the load path of the ClassLoader.


Related articles: