Get classpath path in Java code

Source: Internet
Author: User

Source: http://blog.csdn.net/magi1201/article/details/18731581

Java Web Engineering, sometimes you need to manually read classpath the following configuration file, here to summarize a bit of reading the classpath path, share.

Method One:

String Path = Test.class.getResource ("/"). ToString ();   SYSTEM.OUT.PRINTLN ("path =" + path);  

This method has no problem under Tomcat and can be taken to web-inf/classes

Path = file:/home/ngidm_db2/as_tomcat7_0_29/webapps/ngidm/web-inf/classes/

But after changing the WebLogic, we take the

Path = file:/oracle/weblogic/oracle/middleware/oracle_home/user_projects/domains/base_domain/

Under the WebLogic, failed to get the classes path.

Method Two:


String path2 = Thread.CurrentThread (). Getcontextclassloader (). GetResource ("/"). GetPath ();  System.out.println ("path2 =" + path2);  

  

Under Tomcat and WebLogic, it is advisable to go to the classes path

path2 =/oracle/weblogic/oracle/middleware/user_apps/ngidm/web-inf/classes/path2 =/home/ngidm_db2/as_tomcat7_0_29 /webapps/ngidm/web-inf/classes/

  

Therefore, we recommend that we use more

Thread.CurrentThread (). Getcontextclassloader (). GetResource ("/"). GetPath ();

Gets the classpath path, and Method 2 takes the Classpath without the file: prefix, which can be used directly.

Get classpath path in Java code

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.