This article was reproduced from: http://blog.163.com/michaelgaoit%40126/blog/static/11389538620103711613620/
Running on the web
1:
This.getclass (). getClassLoader (). GetResource ("/"). GetPath ();
This.getclass (). getClassLoader (). GetResource (""). GetPath (); The absolute URI path of the classpath is obtained.
such as:/d:/jboss-4.2.2.ga/server/default/deploy/hp.war/web-inf/classes/
System.getproperty ("User.dir");
This.getclass (). getClassLoader (). GetResource ("."). GetPath (); The absolute path to the project is obtained.
such as:/d:/jboss-4.2.2.ga/server/default/deploy/hp.war
2:
This.getclass (). GetResource ("/"). GetPath ();
This.getclass (). GetResource (""). GetPath (); Gets the URI directory of the current class file. Don't include yourself!
such as:/d:/jboss-4.2.2.ga/server/default/deploy/hp.war/web-inf/classes/com/jebel/helper/
This.getclass (). GetResource ("."). GetPath (); X cannot be run
3:
Thread.CurrentThread (). Getcontextclassloader (). GetResource ("/"). GetPath ()
Thread.CurrentThread (). Getcontextclassloader (). GetResource (""). GetPath () Gets the absolute URI path of the classpath.
such as:/d:/jboss-4.2.2.ga/server/default/deploy/hp.war/web-inf/classes/
Thread.CurrentThread (). Getcontextclassloader (). GetResource ("."). GetPath () Gets the absolute path of the project.
such as:/d:/jboss-4.2.2.ga/server/default/deploy/hp.war
In the local run
1:
This.getclass (). getClassLoader (). GetResource (""). GetPath ();
This.getclass (). getClassLoader (). GetResource ("."). GetPath (); The absolute URI path of the classpath is obtained.
such as:/d:/myprojects/hp/webroot/web-inf/classes
This.getclass (). getClassLoader (). GetResource ("."). GetPath (); X cannot be run
2:
This.getclass (). GetResource (""). GetPath ();
This.getclass (). GetResource ("."). GetPath (); Gets the URI directory of the current class file. Don't include yourself!
such as:/d:/myprojects/hp/webroot/web-inf/classes/com/jebel/helper/
/d:/myprojects/hp/webroot/web-inf/classes/gets the absolute URI path of the classpath.
such as:/d:/myprojects/hp/webroot/web-inf/classes
3:
Thread.CurrentThread (). Getcontextclassloader (). GetResource ("."). GetPath ()
Thread.CurrentThread (). Getcontextclassloader (). GetResource (""). GetPath () Gets the absolute URI path of the classpath:
such as:/d:/myprojects/hp/webroot/web-inf/classes
Thread.CurrentThread (). Getcontextclassloader (). GetResource ("/"). GetPath () X cannot run
At last
In a Web application, we generally get the absolute path to the root of the Web application through the Servletcontext.getrealpath ("/") method.
There are also Request.getcontextpath (); In WebLogic, use Request.getservletcontext (). Getcontextpath (); But if packaged into a war to deploy to a WebLogic server, there is no concept of a file structure inside the project, and in this way always get null , the path is not available and no specific solution has been found yet.
Reprint: Get project file path in Java