Download Ava Web project, sometimes you need to manually read classpath the following configuration file, here to summarize a bit of the way to read the classpath path, share a bit.
Method One:
Java code Download
- String path = Test.Class.getResource ("/"). ToString ();
- System.out.println ("path =" + path);
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 swapping the WebLogic, the path = file:/oracle/weblogic/oracle/middleware/oracle_home/user_projects/domains/base_domain/is taken. Under the WebLogic, failed to get the classes path.
Method Two:
Java code
- String path2 = Thread.CurrentThread (). Getcontextclassloader (). GetResource ("/"). GetPath ();
- System.out.println ("path2 =" + path2);
String path2 = Thread.CurrentThread (). Getcontextclassloader (). GetResource ("/"). GetPath (); System.out.println ("path2 =" + path2);
Under Tomcat and WebLogic, it is advisable to 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 recommended that we use more
Java code
- Thread.CurrentThread (). Getcontextclassloader (). GetResource ("/"). GetPath ();
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