Original website: http://blog.csdn.net/shendl/article/details/1427475(Note: Use the following method to get the path, if there is a space character in the path, then there will be "%20" to replace, so you can then replace "%20 " with a space)Here are some ways to get the absolute path of classpath and the current class. You may need to use some of these methods to get the absolute path of the resources you need.
1
, FileTest.class.getResource ("");
FileTest.class.getResource (".. /");//Get the upper level The URI directory of the current class Filetest.class file is obtained. Don't include yourself! 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 resulting absolute URI path is also the current classpath. such as: file:/d:/java/eclipse32/workspace/jbpmtest3/bin/
4
, FileTest.class.getClassLoader (). GetResource ("")The resulting absolute URI path is also the current classpath. such as: file:/d:/java/eclipse32/workspace/jbpmtest3/bin/
5
, Classloader.getsystemresource ("")The resulting absolute URI path is also 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.
Some methods in Java that get the absolute path of the classpath and the current class (%20 in the path replaces spaces)