Reprint: Get project file path in Java

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.