weblogic获取classes下配置文件路径

本文介绍了在WebLogic服务器环境中如何正确地加载位于_wl_cls_gen.jar中的配置文件,避免了因路径问题导致的文件加载失败,并提供了一个具体的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

网上的办法都不靠谱,没说到点上。
在发布war包时,weblogic会自动把classes下面的文件编译成一个_wl_cls_gen.jar 文件,放在WEB-INF/lib下面。
这时,我们不能使用先获取文件路径,再通过new File(filePath)的方式获取文件。因为获取到的路径是以zip://开头的。
例如:
String path = this.class.getClassLoader().getResource("config/config.properties").getPath();
File file = new File(path);
直接获取文件流就可以了。
ClassLoader classloader =Thread.currentThread().getContextClassLoader();
InputStream is = classloader.getResourceAsStream("config/config.properties");
说明:config/config.properties:config.properties是在_wl_cls_gen.jar的config目录下。
以下是源码:
//获取ClassLoader
ClassLoader classloader =Thread.currentThread().getContextClassLoader();
String aae100="";
InputStream is = null;
try {
is = classloader.getResourceAsStream("config/config.properties");
Properties prop = new Properties();
prop.load(is);
aae100 = prop.getProperty("aae100");
}catch (Exception e) {
e.printStackTrace();
}finally {
if (is != null) {
try {
is.close();
}catch (Exception e) {
e.printStackTrace();
}
}
}
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值