JAVA Appliation下取得资源文件的路径
?
?
那么如果得到这个文件呢?先看下面的一个对路径的测试代码
import java.io.File;public class PathTest { public static void main(String[] args) throws Exception { System.out.println( Thread.currentThread().getContextClassLoader().getResource("")); System.out.println(PathTest.class.getClassLoader().getResource("")); System.out.println(ClassLoader.getSystemResource("")); System.out.println(PathTest.class.getResource("")); System.out.println(PathTest.class.getResource("/")); System.out.println(new File("").getAbsolutePath()); System.out.println(System.getProperty("user.dir")); }}?private static Element getRootElement() throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(ConfigFactoryImplTest. class .getResource("") + "boss-connector-service.xml"); return doc.getDocumentElement(); }?
?
后记:
得到资源文件的路径,在不同的环境(如:Eclipse RCP、Tomcat、Weblogic、简单的Java Application),方法都是略有不同的。具体的环境要具体分析。