In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you what are the classification of Java paths and points for attention. I hope you will get something after reading this article. Let's discuss it together.
The simple Java path can be divided into two types: absolute path and relative path. Here are some details about the classification of Java paths and the issues you need to pay attention to when using Java paths.
Specifically, there are four types of Java paths:
1. Absolute resource paths in the form of URI such as file:/D:/java/eclipse32/workspace/jbpmtest3/bin/aaa.b URL is a special case of URI. The prefix / protocol of URL must be recognized by Java. URL can open resources, but URI cannot. URL and URI objects can be converted to each other using their respective toURI () and toURL () methods.
Second, the absolute path of the local system in the D:/java/eclipse32/workspace/jbpmtest3/bin/aaa.b Java.io package of the class, need to use this form of parameters. However, they generally also provide parameters of type URI, while parameters of type URI accept URI-style String. Therefore, with URI transformation, you can still use URI-style absolute paths in classes in the java.io package.
Third, the relative path relative to classpath, such as the relative path relative to file:/D:/java/eclipse32/workspace/jbpmtest3/bin/. Among them, bin is the classpath of this project. All compiled .class files from the Java source files are copied to this directory.
The relative path relative to the current user directory is the path returned relative to System.getProperty ("user.dir"). For a general project, this is the root path of the project. For JavaEE servers, this may be a path to the server. There is no uniform specification for this. Therefore, never use a relative path relative to the current user directory.
However: by default, classes in the java.io package always analyze relative pathnames based on the current user directory.
This directory is specified by the system property user.dir and is usually the call directory of the Java virtual machine.
That is, when using classes in the java.io package, * do not use relative paths. Otherwise, while it may be normal in a J2SE application, something will go wrong in the J2EE program. And this path is different in different servers. The relative path * practice recommends using a relative path relative to the current classpath. Therefore, when we use a relative path, we should use a relative path relative to the current classpath.
Methods such as getResource (String name) and getResourceAsStream (String name) of the ClassLoader class use relative paths relative to the classpath of the current project to find resources. The same is true of the getBundle (String path) of the ResourceBundle class commonly used to read properties files. By looking at the source code of the ClassLoader class and its related classes, I found that it actually uses an absolute path in the form of URI. By obtaining the absolute path in the form of URI of the current classpath, the absolute path in the form of URI of the relative path is constructed. (this is actually a guess, because JDK invokes SUN's source code internally, which is not part of JDK and is not open source. Relative paths are essentially absolute paths, so in the final analysis, Java essentially can only use absolute paths to find resources. All the relative paths to find resources are just convenient methods. It's just that API helps us build absolute paths at the bottom to find resources. Some ways to get the absolute path to classpath and the current class here are some ways to get the absolute path to classpath and the current class.
Java path: you may need to use some of these methods to get the absolute path to the resources you need.
1fileTest.class.getResource ("") gets the URI directory of the current class FileTest.class file. Not including yourself. Such as: file:/D:/java/eclipse32/workspace/jbpmtest3/bin/com/test/
2FileTest.class.getResource ("/") gets the absolute URI path of the current classpath. Such as: file:/D:/java/eclipse32/workspace/jbpmtest3/bin/
3Jing Thread.currentThread (). GetContextClassLoader (). GetResource ("") also gets the absolute URI path of the current ClassPath. Such as: file:/D:/java/eclipse32/workspace/jbpmtest3/bin/
4what getResource FileTest. Class. GetClassLoader (). URI ("") gets is also the absolute URI path of the current ClassPath. Such as: file:/D:/java/eclipse32/workspace/jbpmtest3/bin/
5Gravity ClassLoader.getSystemResource ("") also gets the absolute URI path of the current ClassPath. For example: file:/D:/java/eclipse32/workspace/jbpmtest3/bin/ I recommend using Thread.currentThread (). GetContextClassLoader (). GetResource ("") to get the URI representation of the absolute path of the current classpath. Resource addressing in the Web application, as mentioned above, the current user directory, that is, the path returned relative to System.getProperty ("user.dir").
For JavaEE servers, this may be a path to the server, and there is no uniform specification.
Not the root directory of the Web application we released.
In this way, we must not use a relative path relative to the current user directory in a Web application.
In Web applications, we usually get the absolute path to the root directory of the Web application through the ServletContext.getRealPath ("/") method. In this way, we only need to provide a path relative to the root directory of the Web application to build an absolute path to locate the resource. This is the strategy we generally adopt when developing Web applications.
General relative path solution there are many kinds of relative paths in Java, which are not easy to use and are very error-prone. Therefore, I wrote a convenient method to help solve the relative path problem more easily.
Resource addressing problems running with JavaSE in Web applications in JavaSE programs, we generally use classpath as the destination for storing resources. However, in Web applications, we generally use WEB-INF and its subdirectories outside the classpath as the storage place for resource files. In Web applications, we usually get the absolute path to the root directory of the Web application through the ServletContext.getRealPath ("/") method. In this way, we only need to provide a path relative to the root directory of the Web application to build an absolute path to locate the resource.
Web applications, which can be published and run as Web applications.
However, we also often run the main method of a class in a Web application as JavaSE.
Or, use the JUnit test. All of this needs to be run using JavaSE.
In this way, we cannot use the ServletContext.getRealPath ("/") method to get the absolute path to the root directory of the Web application.
The ClassLoader class provided by JDK, its methods such as getResource (String name), getResourceAsStream (String name), and so on, use a relative path relative to the classpath of the current project to find resources. The same is true of the getBundle (String path) of the ResourceBundle class commonly used to read properties files. All of them can only use relative paths to read resources under classpath and cannot locate resources outside classpath. The problem of reading configuration files outside Classpath, for example, we will encounter problems when we use test-driven development to develop Web applications that use configuration files, such as Spring, Hibernate, iBatis, and so on. Although Spring itself provides FileSystem (that is, relative to the user,dir directory) to read Web configuration files, it is not very convenient after all. And it is inconsistent with the way the code is used in Web programs. As for Hibernate,iBatis, it is more troublesome. It is impossible to use test-driven development unless you move the configuration file under classpath.
After reading this article, I believe you have a certain understanding of "what are the classification and precautions for the use of Java paths". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.