String path = ConnPool.class.getPackage().getName().replaceAll("//.", "/")+"/Config.xml";
URL url = null;
try {
if (Thread.currentThread().getContextClassLoader() != null) {
url = Thread.currentThread().getContextClassLoader().getResource(path);
}
if (url == null) {
url = ConnPool2.class.getClassLoader().getResource(path);
}
if(url ==null) {
url = ConnPool2.class.getClassLoader().getResource("/"+path);
}
if(url != null) {
JAXPConfigurator.configure(url.getFile(), false);
}
else{
String strURL = ConnPool2.class.getProtectionDomain().getCodeSource().getLocation().getPath();
strURL += ConnPool.class.getPackage().getName().replaceAll("//.", "/")+"/Config.xml";
JAXPConfigurator.configure(strURL, false);
}
} catch (Exception e) {
try {
InputStream in = ConnPool.class.getResourceAsStream(path);
BufferedReader reader= new BufferedReader(new InputStreamReader(in));
} catch (Exception ex) {
InputStream in = ConnPool.class.getResourceAsStream("/"+path);
BufferedReader reader= new BufferedReader(new InputStreamReader(in));
}
}
try {
Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
从Jar包获取资源的方法
最新推荐文章于 2024-06-02 17:15:32 发布
本文介绍了一种在Java环境中定位并加载配置文件的方法。通过使用不同类加载器及资源路径处理技巧,确保应用程序能够从多种位置读取到配置文件。此外,还展示了如何处理配置文件读取过程中可能出现的异常情况。
1万+

被折叠的 条评论
为什么被折叠?



