The difference between Java's class.getresource and Classloader.getresource

Source: Internet
Author: User

Java to take resources, often used class.getresource and classloader.getresource, here to see their resource files when the path problem.

Class.getResource (String path)

When path does not start with '/', the default is to take resources from the package where the class resides;
When path begins with '/', it is obtained from the classpath root;

What does that mean? Look at the output of the following code to see:

Package testpackage;
public class Testmain {
public static void Main (string[] args) {
System.out.println (TestMain.class.getResource (""));
System.out.println (TestMain.class.getResource ("/"));
}
}


Output results:

file:/e:/workspace/test/bin/testpackage/
file:/e:/workspace/test/bin/

As mentioned above, "path begins with '/' and is fetched from the classpath root," which is equivalent to the bin directory (under the Eclipse environment).

Let's take another example, assuming the following project structure:



O_packagestructure1o_packagestructure1

How do we write a path if we want to take the 1~3.properties file separately in the Testmain.java? The code is as follows:

Package testpackage;

public class Testmain {

public static void Main (string[] args) {
The package directory where the current class (class) resides
System.out.println (TestMain.class.getResource (""));
Class Path root directory
System.out.println (TestMain.class.getResource ("/"));

Testmain.class in the <bin>/testpackage bag.
2.properties in <bin>/testpackage Package
System.out.println (TestMain.class.getResource ("2.properties"));

Testmain.class in the <bin>/testpackage bag.
3.properties in <bin>/testpackage.subpackage Package
System.out.println (TestMain.class.getResource ("subpackage/3.properties"));

Testmain.class in the <bin>/testpackage bag.
1.properties in Bin directory (class root directory)
System.out.println (TestMain.class.getResource ("/1.properties"));
}

}


※class.getresource and Class.getresourceasstream in use, the path selection is the same.


Class.getclassloader (). GetResource (String path)

Path cannot begin with '/';
The path is obtained from the classpath root;


Let's take a look at the output of the following piece of code:

Package testpackage;
public class Testmain {
public static void Main (string[] args) {
Testmain t = new Testmain ();
System.out.println (T.getclass ());
System.out.println (T.getclass (). getClassLoader ());
System.out.println (T.getclass (). getClassLoader (). GetResource (""));
System.out.println (T.getclass (). getClassLoader (). GetResource ("/"));//null
}
}



Output results:

Class Testpackage. Testmain
Sun.misc.launcher$appclassloader@1fb8ee3
file:/e:/workspace/test/bin/
Null


From the result "TestMain.class.getResource ("/") = = T.getclass (). getClassLoader (). GetResource (" ")"

If you have the same project structure



O_packagestructureo_packagestructure

uses Class.getclassloader (). GetResource (String path) to write this:

Package testpackage;

public class Testmain {
public static void Main (string[] args) {
Testmain t = new Testmain ();
System.out.println (T.getclass (). getClassLoader (). GetResource (""));

    System.out.println (T.getclass (). getClassLoader (). GetResource ("1.properties"));
    System.out.println (T.getclass (). getClassLoader (). GetResource ("testpackage/2.properties"));
    System.out.println (T.getclass (). getClassLoader (). GetResource ("TESTPACKAGE/SUBPACKAGE/3. Properties "));
}

}

 
※ Class.getclassloader (). GetResource and Class.getclassloader (). getResourceAsStream in use, the path selection is the same.

Related Article

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.