获取路径的方法:
System.out.println("path1:"+this.getClass().getResource("/").getPath().replaceAll("%20", " "));
System.out.println("path2:"+Thread.currentThread().getContextClassLoader().getResource("").toString().replaceAll("%20", " "));
System.out.println("path3:"+MyTest.class.getClassLoader().getResource("").toString().replaceAll("%20", " "));
System.out.println("path4:"+ClassLoader.getSystemResource(""));
System.out.println("path5:"+MyTest.class.getResource("").toString().replaceAll("%20", " "));
String tmp = MyTest.class.getResource("").toString().replaceAll("%20", " ");
String str = tmp.substring(tmp.indexOf(":")+1,tmp.indexOf("/WEB-INF/classes/")+"/WEB-INF/classes/".length());
System.out.println("path6:"+str);
System.out.println("path7:"+MyTest.class.getResource("/").toString().replaceAll("%20", " "));
System.out.println("path8:"+new File("/").getAbsolutePath());
System.out.println("path9:"+System.getProperty("user.dir"));
tmp = System.getProperty("user.dir");
str = "/"+tmp.replaceAll("\\\\", "/")+"/";
System.out.println("path10:"+str);
Tomcat 服务器中输出信息:
path1:/C:/tool/Tomcat 6.0/webapps/test/WEB-INF/classes/
path2:file:/C:/tool/Tomcat 6.0/webapps/test/WEB-INF/classes/
path3:file:/C:/tool/Tomcat 6.0/webapps/test/WEB-INF/classes/
path4:null
path5:file:/C:/tool/Tomcat 6.0/webapps/test/WEB-INF/classes/com/zht/test/
path6:/C:/tool/Tomcat 6.0/webapps/test/WEB-INF/classes/
path7:file:/C:/tool/Tomcat 6.0/webapps/test/WEB-INF/classes/
path8:C:\
path9:C:\tool\Tomcat 6.0\bin
path10:/C:/tool/Tomcat 6.0/bin/
Weblogic 服务器中输出信息:
path1:/C:/tool/BEA/weblogic92/common/lib/
path2:file:/C:/tool/BEA/weblogic92/common/lib/
path3:file:/C:/tool/BEA/weblogic92/common/lib/
path4:file:/C:/tool/BEA/weblogic92/common/lib/
path5:file:/C:/tool/BEA/user_projects/domains/Firstdomain/autodeploy/test/WEB-INF/classes/com/zht/test/
path6:/C:/tool/BEA/user_projects/domains/Firstdomain/autodeploy/test/WEB-INF/classes/
path7:file:/C:/tool/BEA/weblogic92/common/lib/
path8:C:\
path9:C:\tool\BEA\user_projects\domains\Firstdomain
path10:/C:/tool/BEA/user_projects/domains/Firstdomain/