In java, there are N methods to obtain the file path. Next I will introduce some examples of obtaining the path to help you.
1. Obtain the path from the java File
Thread. currentThread (). getContextClassLoader (). getResource ("") // obtain the path of the resource file (. class file)
ClassLoader. getSystemResource ("")
Class_Name.class.getClassLoader (). getResource ("")
Class_Name.class. getResource ("/")
Class_Name.class. getResource ("") // obtain the path of the current class
System. getProperty ("user. dir") // obtain the absolute path of the Project root directory
System. getProperty ("java. class. path") // obtain the class path and package path.
The output is as follows:
File:/F:/work_litao/uri_test/WebContent/WEB-INF/classes/
File:/F:/work_litao/uri_test/WebContent/WEB-INF/classes/
File:/F:/work_litao/uri_test/WebContent/WEB-INF/classes/
File:/F:/work_litao/uri_test/WebContent/WEB-INF/classes/
File:/F:/work_litao/uri_test/WebContent/WEB-INF/classes/com/xml/imp/
F: work_litaouri_test
F: work_litaouri_testWebContentWEB-INFclasses; F: work_litaouri_testWebContentWEB-INFlibdom4j.jar
2. Obtain the relative and absolute paths of the current application in JSP.
Absolute path corresponding to the root directory: request. getRequestURI ()
Absolute path of the file: application. getRealPath (request. getRequestURI ());
Absolute path of the current web application: application. getRealPath ("/");
Obtain the upper-level directory of the request File: new File (application. getRealPath (request. getRequestURI (). getParent ()
3.1 obtain the relative and absolute paths of the current application in JSP
Absolute path corresponding to the root directory: request. getRequestURI ()
Absolute path of the file: application. getRealPath (request. getRequestURI ());
Absolute path of the current web application: application. getRealPath ("/");
Obtain the upper-level directory of the request File: new File (application. getRealPath (request. getRequestURI (). getParent ()
3. Obtain the relative and absolute paths of the current application in Servlet.
Absolute path corresponding to the root directory: request. getServletPath ();
Absolute path of the file: request. getSession (). getServletContext (). getRealPath (request. getRequestURI ())
Absolute path of the current web application: servletConfig. getServletContext (). getRealPath ("/");
(The ServletContext object can be obtained in the following ways:
Javax. servlet. http. HttpSession. getServletContext ()
Javax. servlet. jsp. PageContext. getServletContext ()
Javax. servlet. ServletConfig. getServletContext ()
)
ClassLoader. getSystemResource ()
Search for resources with the specified name from the search path used to load the class. This method uses the system class loader to find resources. The URL object used to read resources. The URL type is returned. If no resource is found, null is returned.
The parameter starts with "/" and is relative to the package path. Otherwise, it indicates the path relative to the class.
ClassLoader. getResource ()
Search for resources with a given name. Resources are some data (images, sounds, text, etc.) that can be accessed in a way unrelated to the code base through class code ). Resource names are separated.
This method first searches for the parent class loader of the resource. If the parent class loader is null, the search path is the path of the built-in class loader of the virtual machine. If the search fails, findResource (String) is called to find the resource. The URL object used to read the resource. The URL type is returned. If the resource cannot be found or the caller does not have sufficient permissions to obtain the resource, null is returned.
File. getAbsolutePath ()
Returns the absolute path name string of the abstract path.
If the abstract path name is already an absolute path name, the return path name string, which is the same as the getPath () method. If the abstract path name is null, the path name string of the current user directory is returned, which is specified by the system property user. dir. Otherwise, resolve the path name in a system-related manner. On UNIX systems, the relative path name can be parsed based on the current user directory to make this path an absolute path name. In Windows, the relative path name can be parsed based on the current drive directory specified by the path name (if any) to make it an absolute path name; otherwise, it can be parsed based on the current user directory. Returns an absolute path name string that represents the same file or directory as the abstract path name.
System. getProperty ("user. dir ")
Obtain the current project path of the user. Because all classes in java. io interpret the relative path name as the current working directory of the user, the current directory should be clear.
Class. getResource ()
Search for resources with a given name. The rule for searching resources related to a given class is implemented by defining the class loader of the class. Returns a URL object. If no resource with this name is found, null is returned. This method is delegated to the class loader of this object. If this object is loaded through the bootstrap loader, this method is delegated to ClassLoader. getSystemResource (java. lang. String ).
Before delegation, use the following algorithm to construct an absolute resource name from the given Resource Name:
If the name starts with "/" ("/u002f"), the absolute Resource name is part of the name after.
Otherwise, the absolute name has the following form: modified_package_name/name, where modified_package_name is the package name of this object, which replaces "." ("/u002e") ").
Class. getResourceAsStream ()
The API indicates that it is the same as getResource and is delegated to ClassLoader. getSystemResource (name.
Resource file path Structure
"[01]" + ClassLoader. getSystemResource ("")
"[02]" + Thread. currentThread (). getContextClassLoader (). getResource ("")
"[03]" + this. getClass (). getClassLoader (). getResource ("")
"[04]" + this. getClass (). getResource ("Path. class ")
"[05]" + new File ("/"). getAbsolutePath ()
"[06]" + new File (""). getAbsolutePath ()
"[07]" + System. getProperty ("user. dir ")
"[08]" + this. getClass (). getResource ("/test. xml ")
"[09]" + this. getClass (). getResource ("/test. xml"). toString ()
"[10]" + this. getClass (). getResource ("/test. xml"). getFile ()
"[11]" + new File (this. getClass (). getResource ("/test. xml"). toString (). getAbsolutePath ()
"[12]" + new File (this. getClass (). getResource ("/test. xml"). getFile (). getAbsolutePath ()
"[13]" + new InputStreamReader (this. getClass (). getResourceAsStream ("/test. xml "))
"[14]" + this. getClass (). getResource ("test. xml"). toString ()
"[15]" + this. getClass (). getResource ("test. xml"). getFile ()
"[16]" + new File (this. getClass (). getResource ("test. xml"). toString (). getAbsolutePath ()
"[17]" + new File (this. getClass (). getResource ("test. xml"). getFile (). getAbsolutePath ()
"[18]" + new InputStreamReader (this. getClass (). getResourceAsStream ("test. xml "))
Result of executing the class file in windows (the compiled file is in E:/workspace/sunyzc/bin /)
[01] file:/E:/workspace/sunyzc/bin/
[02] file:/E:/workspace/sunyzc/bin/
[03] file:/E:/workspace/sunyzc/bin/
[04] file:/E:/workspace/sunyzc/bin/com/sunyzc/test/Path. class
[05] E :/
[06] E:/workspace/sunyzc/bin
[07] E:/workspace/sunyzc/bin
[08] file:/E:/workspace/sunyzc/bin/test. xml
[09] file:/E:/workspace/sunyzc/bin/test. xml
[10]/E:/workspace/sunyzc/bin/test. xml
[11] E:/workspace/sunyzc/bin/file:/E:/workspace/sunyzc/bin/test. xml
[12] E:/workspace/sunyzc/bin/test. xml
[13] java. io. InputStreamReader @ c17164
[14] file:/E:/workspace/sunyzc/bin/com/sunyzc/test. xml
[15]/E:/workspace/sunyzc/bin/com/sunyzc/test. xml
[16] E:/workspace/sunyzc/bin/file:/E:/workspace/sunyzc/bin/com/sunyzc/test. xml
[17] E:/workspace/sunyzc/bin/com/sunyzc/test. xml
[18] java. io. InputStreamReader @ 1fb8ee3
Execute class in windows
Execute class in windows
Result of executing the jar package in windows (the jar package file is in E:/workspace/sunyzc /)
[01] null
[02] null
[03] null
[04] jar: file:/E:/workspace/sunyzc. jar! /Com/sunyzc/test/Path. class
[05] E :/
[06] E:/workspace/sunyzc
[07] E:/workspace/sunyzc
[08] jar: file:/E:/workspace/sunyzc. jar! /Test. xml
[09] jar: file:/E:/workspace/sunyzc. jar! /Test. xml
[10] file:/E:/workspace/sunyzc. jar! /Test. xml
[11] E:/workspace/sunyzc/jar: file:/E:/workspace/sunyzc. jar! /Test. xml
[12] E:/workspace/sunyzc/file:/E:/workspace/sunyzc. jar! /Test. xml
[13] java. io. InputStreamReader @ 1fb8ee3
[14] jar: file:/E:/workspace/sunyzc. jar! /Com/sunyzc/test. xml
[15] file:/E:/workspace/sunyzc. jar! /Com/sunyzc/test. xml
[16] E:/workspace/sunyzc/jar: file:/E:/workspace/sunyzc. jar! /Com/sunyzc/test. xml
[17] E:/workspace/sunyzc/file:/E:/workspace/sunyzc. jar! /Com/sunyzc/test. xml
[18] java. io. InputStreamReader @ 61de33
Run the jar file in windows.
Run the jar file in windows.
Result of executing the jar package in linux (the jar package file is in/usr/workspace/sunyzc /)
ICAS :~ # Cd/usr/workspace/sunyzc
ICAS:/usr/workspace/sunyzc # java-classpath sunyzc. jar com. sunyzc. test. Path
[01] null
[02] null
[03] null
[04] jar: file:/usr/workspace/sunyzc. jar! /Com/sunyzc/test/Path. class
[05]/
[06]/usr/workspace/sunyzc
[07]/usr/workspace/sunyzc
[08] jar: file:/usr/workspace/sunyzc. jar! /Test. xml
[09] jar: file:/usr/workspace/sunyzc. jar! /Test. xml
[10] file:/usr/workspace/sunyzc. jar! /Test. xml
[11]/usr/workspace/sunyzc/jar: file:/usr/workspace/sunyzc. jar! /Test. xml
[12]/usr/workspace/sunyzc/file:/usr/workspace/sunyzc. jar! /Test. xml
[13] java. io. InputStreamReader @ 1034bb5
[14] jar: file:/usr/workspace/sunyzc. jar! /Com/sunyzc/test. xml
[15] file:/usr/workspace/sunyzc. jar! /Com/sunyzc/test. xml
[16]/usr/workspace/sunyzc/jar: file:/usr/workspace/sunyzc. jar! /Com/sunyzc/test. xml
[17]/usr/workspace/sunyzc/file:/usr/workspace/sunyzc. jar! /Com/sunyzc/test. xml
[18] java. io. InputStreamReader @ 7f5f5897
ICAS:/usr/workspace/sunyzc #
Execute jar files in linux
Execute jar files in linux
In windows tomcat execute class (compile file in E:/workspace/sunyzc/context/WEB-INF/classes /)
[01] null
[02] file:/E:/workspace/sunyzc/context/WEB-INF/classes/
[03] file:/E:/workspace/sunyzc/context/WEB-INF/classes/
[04] null
[05] C :/
[06] C:/Users/sunyzc
[07] C:/Users/sunyzc
[08] null
All the following are null pointers.
In windows, tomcat executes the jar package (tomcat path E:/apache-tomcat-6.0.35 /)
[01] null
[02] file:/E:/apache-tomcat-6.0.35/lib/
[03] file:/E:/apache-tomcat-6.0.35/lib/
[04] null
[05] C :/
[06] C:/Users/sunyzc
[07] C:/Users/sunyzc
[08] null
All the following are null pointers.
In linux, tomcat executes the jar package (tomcat path/usr/sharescm/tomcat6.0.26 /)
[01] null
[02] file:/usr/sharescm/LCD/WEB-INF/classes/
[03] file:/usr/sharescm/LCD/WEB-INF/classes/
[04] null
[05]/
[06]/root
[07]/root
[08] null
All the following are null pointers.