Summarize some ways to take Java paths Online:
Note: If you start the program from Ant, This.getclass () getresource ("") to take out the strange, directly with the Java command Line debugging can be successful.
Some methods to get classpath and the absolute path of the current class
To obtain a path beyond Classpath:
URL base = This.getclass (). GetResource ("");///First get the location of this class, such as/home/popeye/testjava/build/classes/net/string path = new File (Base.getfile (), ".../....../....../" +name). Getcanonicalpath ();//You can get/home/popeye/testjava/name
Here are some ways to get 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 resources you need.
1.filetest.class.getresource ("")
Gets the URI directory of the current class Filetest.class file. Not including myself!
such as: file:/d:/java/eclipse32/workspace/jbpmtest3/bin/com/test/
2.filetest.class.getresource ("/")
Gets the absolute URI path of the current classpath.
such as: file:/d:/java/eclipse32/workspace/jbpmtest3/bin/
3.thread.currentthread (). Getcontextclassloader (). GetResource ("")
The absolute URI path of the current classpath is also obtained.
such as: file:/d:/java/eclipse32/workspace/jbpmtest3/bin/
4.filetest.class.getclassloader (). GetResource ("")
The absolute URI path of the current classpath is also obtained.
such as: file:/d:/java/eclipse32/workspace/jbpmtest3/bin/
5.classloader.getsystemresource ("")
The absolute URI path of the current classpath is also obtained.
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 typically get the absolute path of the Web application's root directory through the Servletcontext.getrealpath ("/") method. In this way, we only need to provide a path relative to the Web application root directory, we can build the absolute path of locating resources.
Note the point:
1. Try not to use relative paths to the current user directory as opposed to System.getproperty ("User.dir"). This is a ticking time bomb, it may kill you at any moment.
2. Use absolute path resources in the form of URIs as far as possible. It can be easily transformed into a Uri,url,file object.
3. Use relatively classpath relative path as far as possible. Do not use absolute paths. The public static URL Getextendresource (String relativepath) method of the Classloaderutil class above can already be used to locate resources in all locations using relative paths to classpath.
4. Never use hard coded absolute paths. Because we can completely use the GetResource ("") Method of the ClassLoader class to get the absolute path of the current classpath.
Using hard coded absolute paths is totally unnecessary! It's going to make you die hard to see! The program will not be ported!
If you have to specify an absolute path, then using a configuration file is much better than hard coding!
Of course, I still recommend that you use the program to get the absolute path of classpath to spell the absolute path of the resource.