怎样使jar包里的类能读取同在此jar包中的一个配置文件
来源:互联网 发布:中国检察网络培训学校 编辑:程序博客网 时间:2024/04/29 05:30
问题:获取路径问题
单独项目:
// Properties prop = loadProperties("config/" + fileName);
单独项目property-core:
//Properties prop = loadProperties("src/main/resources/config/" + fileName);这样获取没问题
如果把property-core 作为jar包引入到property-wechat-service里面时:会报错,找不到该系统文件。
最后是这种形式: Properties prop = loadProperties(path0+"config/" + config.properties);
path0即是classes路径。
观察编译后的文件可以知道文件都在classes下面。
故而应该转变思路,先获取该类编译后的路径,然后获取classes路径,最后根据配置文件路径来获取该配置文件的路径。
思路:所有的文件编译后都会放在classes目录下,我们获取这个目录就好了。
public class ConfigHelper {
String path= this.getClass().getResource("").getPath();//获取当前编译类的路径
System.out.println("--------------++++++++++1-------------"+path);
String path2= this.getClass().getResource("/").getPath();
System.out.println("--------------++++++++++2-------------"+path2);
int n=path.indexOf("com");
String path0=path.substring(0, n);//根据当前编译类的路径获取classes路径
System.out.println("--------------++++++++++0-------------"+path0);
Properties prop = loadProperties(path0+"config/" + config.properties);
}
控制台打印:
--------------++++++++++1-------------/F:/information20170320/source/property-parent/property-core/target/classes/com/tianjian/property/jieshun/utils/
--------------++++++++++2-------------/F:/information20170320/source/property-parent/property-wechat-service/target/classes/
--------------++++++++++0-------------/F:/information20170320/source/property-parent/property-core/target/classes/
- 怎样使jar包里的类能读取同在此jar包中的一个配置文件
- jar包里的类读取配置文件
- 读取Jar包中的配置文件
- jar包想读取jar包以外的配置文件
- jar包读取jar包内部和外部的配置文件
- 读取JAR包中配置文件的解决方法
- 读取jar包内外的配置文件
- jar包中配置文件路径的读取
- 读取JAR包中配置文件的解决方法
- 如何读取Jar包里的文件
- 读取jar包里的资源文件
- java读取jar包配置文件
- 加载jar包中的配置文件
- 修改jar包中的配置文件
- spring里,load jar包里的xml配置文件问题
- SpringMVC + mybatis 无法读取JAR包中的XML配置文件问题
- SpringMVC + mybatis 无法读取JAR包中的XML配置文件问题
- 读取jar包中的文件
- java多线程之线程的优先级
- LeetCode(3)
- Adnroid弹出框属性大全
- java基础
- python3 读写Excel
- 怎样使jar包里的类能读取同在此jar包中的一个配置文件
- js定义对象循环拿到value和key
- 嵌入式的发展方向
- iOS耳机红外线遥控器
- php操作毫秒时间戳
- 策略模式【Strategy Pattern】—— 锦囊妙计
- 间距
- Android 弹幕可用开源框架
- java利用POI通过模板导出excel的一个实例