The methods getResourceAsStream("") and getResource("") are often used to get the configuration file specified in the compilation path. The usage is similar. The following uses getResource("") as an example to show its correct usage:
/**
* no package
*/
System.out.println(this.getClass().getResource("template.xml").getPath());//relative to the template.xml under the current Class path
System.out.println(this.getClass().getResource("/template.xml").getPath());//classpath template.xml under the root path
System.out.println(this.getClass().getClassLoader().getResource("template.xml").getPath());//classpath template.xml under the root path
/**
* package
*/
System.out.println(this.getClass().getResource("package/template.xml").getPath());//relative to the template.xml under the current Classpath/package path
System.out.println(this.getClass().getResource("/package/template.xml").getPath());//classpath template/xml under root path/package path
System.out.println(this.getClass().getClassLoader().getResource("package/template.xml").getPath());//classpath template/xml under the root path/package path
Wrong usage (note the slash):
/**
* no package
*/
System.out.println(this.getClass().getClassLoader().getResource("/template.xml").getPath());//classpath template.xml under the root path
/**
* package
*/
System.out.println(this.getClass().getClassLoader().getResource("/package/template.xml").getPath());//classpath template/xml under the root path/package path
Note: When getting the configuration file in the Jar package, it is recommended to encode by getResourceAsStream("").
JAVA basic popularity:
Classloader class loader for loading Java classes into a Java virtual machine. Different from ordinary programs. Java programs (class files) are not native executables. When running a Java program, first run the JVM (Java Virtual Machine), then load the Java class into the JVM, the part responsible for loading the Java class is called Class Loader.
The JVM itself contains a ClassLoader called Bootstrap ClassLoader. Like the JVM, the BootstrapClassLoader is implemented in native code and is responsible for loading the core JavaClass (that is, all classes starting with java.*). In addition, the JVM will also provide two ClassLoaders, which are written in Java and loaded by BootstrapClassLoader. The Extension ClassLoader is responsible for loading the extended Java class (for example, all classes starting with javax.* and classes stored in the JRE's ext directory). The ApplicationClassLoader is responsible for loading the application's own classes.
When running a program, the JVM starts, runs bootstrapclassloader, the ClassLoader loads the java core API (ExtClassLoader and AppClassLoader are also loaded at this time), then calls ExtClassLoader to load the extension API, and finally AppClassLoader loads the Class defined in the CLASSPATH directory, which is The most basic loading process of a program.
Best Wishes For You!
To solve this problem is very simple: windows version press the shortcut key alt + shift + ctrl + s mac:command + ; Enter the module configuration, select the module you are using, click sources, crea...
The method uses a ClassLoader getResource utf-8 encoded path information, when the pathThe presence of Chinese and space, he will convert these characters, so that, more often than not get what we wan...
GetresourceASSTREAM in Java Return to NULL problem processing: The main reason for this problem is the setting of the source: In our commonly used IDEA, the input stream returned with GetResourceASS...
ClassLoader, a small-name class loader, sounds so tall, the explanation of the online god is esoteric, just getting started, it is difficult to understand. My understanding of it is to load some files...
Directory Structure: debug finds that in is empty, the code is not wrong, only the content of the db.properties file is not read The first feeling is that there may be a problem with the resources fil...
Directly on the map, to facilitate comparison I do not understand even a path in which the above configuration, oh, on the map, even though some of my colleagues in the eyes of eclise have become anti...
[size=large][b]Class's getResourceAsStream(String path)[/b][/size] 1) Relative and absolute paths can be used, and absolute paths start with '/', such as 2) The relative path is the file under the cur...
Foreword In java there are some differences in resource acquisition path, commonly used getResource and getResourceAsStream 1.Class.getResource(String path) path not to '/' a...
This is a very basic question, here are some examples to help you quickly understand and remember the problem...