Details based on methods for getting JAVA paths including paths outside the CLASSPATH

  • 2020-04-01 01:58:08
  • OfStack

Summarize some methods of taking JAVA path online:
Note: if this. GetClass (). GetResource ("") is taken from the ANT launcher, you can successfully debug it directly from the JAVA command line.
Some method to get the classpath and the absolute path of the current class
Method to get a path outside the CLASSPATH:
URL base = this.getclass ().getresource (""); / / get the location of this class, such as/home/popeye testjava/build/classes/net/String path = new File (base. The getFile (), "... /... /... /"+name). GetCanonicalPath (); / / get/home/popeye testjava/name
Here are some ways to get the classpath and the absolute path of the current class. You may need to use some of these methods to get the absolute path to the resource you need.
1. FileTest. Class. GetResource (" ")
You get the URI directory for the current filetest.class file. Not including myself!
Such as: file: / D: / Java/eclipse32 / workspace/jbpmtest3 / bin/com/test /
2. FileTest. Class. GetResource ("/")
You get the absolute URI path of the current classpath.
Such as: file: / D: / Java/eclipse32 / workspace/jbpmtest3 / bin /
3. Thread.currentthread (). GetContextClassLoader (). GetResource ("")
You also get the absolute URI path of the current ClassPath.
Such as: file: / D: / Java/eclipse32 / workspace/jbpmtest3 / bin /
4. FileTest. Class. GetClassLoader () getResource (" ")
You also get the absolute URI path of the current ClassPath.
Such as: file: / D: / Java/eclipse32 / workspace/jbpmtest3 / bin /
5. This. GetSystemResource (" ")
You also get the absolute URI path of the current ClassPath.
Such as: 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.
In Web applications, we usually by ServletContext. GetRealPath ("/") in the root directory of the Web application, and get the absolute path. In this way, we can build an absolute path to locate the resource by simply providing a path relative to the Web application's root directory.
Note:
1. Try not to use a path relative to the current user directory of system.getproperty ("user.dir"). It's a ticking time bomb that could kill you at any moment.
2. Try to use absolute path resources in the form of uris. It can be easily converted into uris, urls, and File objects.
3. Try to use a relative path to the classpath. Do not use absolute paths. Using the public static URL getExtendResource (String relativePath) method of the ClassLoaderUtil class above, it is already possible to locate resources for all locations using the relativePath to the classpath.
4. Never use hard-coded absolute paths. Because we can use the getResource ("") method of the ClassLoader class to get the absolute path to the current classpath.
Using hard-coded absolute paths is completely unnecessary! It's gonna kill you! The program will not be portable!
If you must specify an absolute path, using a configuration file is better than hard coding!
Of course, I recommend that you use the program to get the absolute path of the classpath to spell the absolute path of the resource

Related articles: