About the path of Class. getResource and ClassLoader. getResource, class. getclassloader
Class. getResource and ClassLoader. getResource are often used to retrieve resources in Java. Here we will look at their Path Problems When retrieving resource files.
Class. getResource (String path)
When the path does not start with '/', resources are retrieved from the package where the class is located by default; when the path starts with '/', resources are obtained from the ClassPath root;
What does it mean? You can see the output of the following code:
package testpackage;public class TestMain { public static void main(String[] args) { System.out.println(TestMain.class.getResource("")); System.out.println(TestMain.class.getResource("/")); }}
Output result:
file:/E:/workspace/Test/bin/testpackage/file:/E:/workspace/Test/bin/
As mentioned above, when the path starts with '/', it is obtained from the ClassPath root.] Here it is equivalent to the bin directory (in the Eclipse environment ).
Assume that the following Project structure is available for another instance:
Package testpackage; public class TestMain {public static void main (String [] args) {// package directory where the current class (class) is located: System. out. println (TestMain. class. getResource (""); // class path root directory System. out. println (TestMain. class. getResource ("/"); // TestMain. class in the/testpackage package // 2. properties in the/testpackage: System. out. println (TestMain. class. getResource ("2. properties "); // TestMain. class in the/testpackage package // 3. properties IN/testpackage. system. out. println (TestMain. class. getResource ("subpackage/3. properties "); // TestMain. class in the/testpackage package // 1. properties is in the bin directory (class root directory) System. out. println (TestMain. class. getResource ("/1. properties "));}}
※Class. getResource and Class. getResourceAsStream use the same path.
Class. getClassLoader (). getResource (String path)
Path cannot start with '/'; path is obtained from the ClassPath root;
Let's take a look at the output of the following 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 result:
class testpackage.TestMainsun.misc.Launcher$AppClassLoader@1fb8ee3file:/E:/workspace/Test/bin/null
From the result, [TestMain. class. getResource ("/") = t. getClass (). getClassLoader (). getResource ("")]
If the same Project structure exists
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 use the same path.
**************************************** **************************************** * ******* [From another blog]
-Transfer