Solution to the path problem when Java loads a resource file _java

Source: Internet
Author: User

Loading a resource file is more commonly used in two ways:

First, with ClassLoader, said here will have to mention the classification of ClassLoader, Java built-in ClassLoader mainly have three kinds,

The first is the root class loader (bootstrap class loader), written in C + +, which is responsible for loading some of the key Java classes, such as Java.lang.Object and some other run-time code, into memory first. Package to be loaded: BOOTSTRP------>jre/lib/rt.jar

The second is the Extended class loader (Extclassloader), written by the Java class, which is responsible for loading some classes in the JRE into memory. Package to be loaded: Extclassloader---------->jre/lib/ext/*.jar

The third is the Application class loader (Appclassloader) or the System class loader, which is responsible for loading classes in Classpath into memory. The Application class loader can be obtained by Classloader.getsystemclassloader ();


And then again. The inheritance of the class carrier, the class loader is not a parent-child relationship that is vertically inherited, but rather a combination of relationships that can be passed to the ClassLoader by instantiating the instance of the parent ClassLoader as a constructor parameter when the ClassLoader is instantiated.

For more information about ClassLoader, you can search by yourself.

When you get to the Application class loader, you get the resource file, and the call to Loader.getresource (path) can load the resource files under the corresponding path, not with the '/', and the resources in the package can be used as normal folders to '/' separate each package.

such as: URL url2 = Classloader.getsystemclassloader (). GetResource ("Demo/names.ser"); is to get the Names.ser serialization file within the demo package.

Second, with the need to load the current class GetResource method to load, in fact, this method is called to load the class loader to obtain resource files, but is to get the parameters are different.

(1) In order to get the file in the class package, you can access the resource directly in the package using the relative path, such as: Demo1.class.getResource ("Names.ser"), and get the resource in the package containing the Demo1 class file.

(2) to get the resource file outside the package must start with a '/', such as URL url = Demo1.class.getResource ("/demo/names.ser"); Get the Names.ser file in the demo package


In fact, the second way is a package for the first way, which is a resource file loaded with the ClassLoader. Why do you say that? Look at the class of the source code will know:

Copy Code code as follows:

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);
}

Copy Code code as follows:

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 the name value passed in (that is, relative path or absolute path), we see that after resolvename processing, we call the ClassLoader C1 to load, the ClassLoader loading path is not in the form of '/' The beginning of the relative path, it must be resolvename to convert the path, and then look at the ResolveName method, first of all to determine whether the '/' beginning, if the '/' start, the relative path, otherwise is the absolute path, note else this code block, It removes the first character, after you do get rid of it, it conforms to the ClassLoader loading path, and the If block is based on the package path of the current class, and then it is replaced with the '/', and the relative path is added, which also forms the loading path that conforms to the 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.