Home > Java > javaTutorial > Detailed explanation of Java Resource path arrangement

Detailed explanation of Java Resource path arrangement

黄舟
Release: 2017-03-17 10:22:19
Original
2223 people have browsed it

This article mainly introduces the relevant information of Java Resource path sorting and summary. Friends in need can refer to

Java Resource path

First of all, it is very important. There is no standard relative path in Java. Various relative paths to obtain resources are converted into absolute paths based on certain rules.

Then it is also very important. Absolute Do not use absolute paths directly, otherwise it will be ugly

Based on the above two points, the Resource path problem can be summed up in one point: find the base point, that is, in a certain environment (web, j2ee or jar package, etc.) find a stable base point through a suitable method, and then use this base point to find the resource you want

What are the base points in Java? To roughly summarize, there are the following:

1) classpath

If the resource you are looking for is under the classpath, then it is more appropriate to use the classpath as the base point, and The main way to obtain this base point is through ClassLoader. The specific method is ClassLoader.getResource(String name). There are many ways to obtain ClassLoader, such as:

  1. Thread.currentThread().getContextClassLoader()

  2. ##clazz.getClassLoader()

  3. ClassLoader. getSystemClassLoader()


The implementation principle of ClassLoader to find resources is to first

recursivelyLoad the resource from the classpath in the parent classLoader (how to load the JDK in the end is not open source), if the classLoaders at all levels are not If found, call the findResource method to find it, and this method is exposed to the self-made classLoader for implementation, thus giving the opportunity to load resources outside the classpath.

2) The current user directory


is the path returned relative to System.getProperty("user.dir"). For general projects, this is the root path of the project. . For JavaEE servers, this may be some path to the server. There is no unified standard for this! However, by default, classes in the java.io package always analyze relative path names based on the current user directory, such as new File("xxx"), which means finding xxx under the System.getProperty("user.dir") path. document. Therefore, locating files this way may cause porting issues.


3) The root directory of the Web application


In a Web application, we generally get the root directory of the Web application through the ServletContext.getRealPath("/") method The absolute path to the directory.

After mastering the above basic points, you can easily locate the resource you are looking for. However, you must clearly understand that you should not just pursue temporary happiness, regardless of the life and death of future transplants. You must ensure that it can be used in any environment. There is no problem when downloading (j2se or web, windows or

Linux).

The above is the detailed content of Detailed explanation of Java Resource path arrangement. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template