Ad to the left. Text on the right
Here are some methods for obtaining the Java path on the Internet:
Note: If you start the program from ant, this. getclass (). getresource ("") is more strange, you can directly use the Java command line for debugging.
Obtain the absolute path of classpath and the current class.
To obtain a path other than classpath:
URL base = This. getclass (). getresource (""); // first obtain the location of the class, such as/home/Popeye/testjava/build/classes/NET/
String Path = new file (base. getFile (),".. /.. /.. /"+ name ). getCanonicalPath (); // You Can Get/home/Popeye/testjava/name.
The following are some methods to obtain the absolute path of classpath and the current class. You may need to use some of these methods to obtain the absolute path of the resources you need.
1. filetest. Class. getresource ("")
The URI Directory of the current filetest. Class file is obtained. Not yourself!
For example, file:/D:/Java/eclipse32/workspace/jbpmtest3/bin/COM/test/
2. filetest. Class. getresource ("/")
Obtain the absolute URI path of the current classpath.
For example, file:/D:/Java/eclipse32/workspace/jbpmtest3/bin/
3. thread. currentthread (). getcontextclassloader (). getresource ("")
The obtained absolute URI path of the current classpath.
For example, file:/D:/Java/eclipse32/workspace/jbpmtest3/bin/
4. filetest. Class. getclassloader (). getresource ("")
The obtained absolute URI path of the current classpath.
For example, file:/D:/Java/eclipse32/workspace/jbpmtest3/bin/
5. classloader. getsystemresource ("")
The obtained absolute URI path of the current classpath.
For example, file:/D:/Java/eclipse32/workspace/jbpmtest3/bin/
We recommend that you use thread. currentthread (). getcontextclassloader (). getresource ("") to obtain the URI representation of the absolute path of the current classpath.
In Web applications, we generally use the servletcontext. getrealpath ("/") method to obtain the absolute path of the root directory of the Web application. In this way, we only need to provide the path relative to the root directory of the Web application program to build an absolute path to locate the resource.
Note:
1. Try not to use the relative path relative to the current user directory of system. getproperty ("user. dir. This is a time bomb, and you may be killed at any time.
2. Try to use the URI format
. It can easily be converted to Uri, URL, and file objects.
3. Try to use the relative path of the classpath. Do not use absolute paths. Use the public static URL of the classloaderutil class above
Getextendresource (string
The relativepath method can use relative paths relative to classpath to locate resources in all locations.
4. Do not use hard-coded absolute paths. Because we can use the getresource ("") method of the classloader class to obtain the absolute path of the current classpath.
Using a hard-coded absolute path is completely unnecessary! It will make your death ugly! The program cannot be transplanted!
If you must specify an absolute path, it is much better to use the configuration file than hard encoding!
Of course, I recommend that you use the program to obtain the absolute path of classpath to spell the absolute path of resources.