Python 官方文档:入门教程 => 点击学习
案例:本文主要描述linux系统执行jar包读取jar包同级目录的外部配置文件 方法一:相对路径设置配置文件 (1)在jar包同级目录创建配置文件conf.properties并写入
案例:本文主要描述linux系统执行jar包读取jar包同级目录的外部配置文件
方法一:相对路径设置配置文件
(1)在jar包同级目录创建配置文件conf.properties并写入配置数据:
confData=data
(2)开始写入自动化测试代码
//from www.fhadmin.cn
public class Test{
public String getData() throws ioException {
//读取配置文件
Properties properties = new Properties();
File file = new File("conf.properties");
FileInputStream fis = new FileInputStream(file);
properties.load(fis);
fis.close();
//获取配置文件数据
String confData = properties.getProperty("confData");
System.out.println(confData);
}
}
(3)执行jar包
java -jar jarNanexxx
方法二:绝对路径设置配置文件
解决问题:使用相对路径的方法在jar包同级目录手动执行jar包时没有问题,但使用linux系统的crontab文件定时调度时报错,原因:因为我们手动执行某个脚本时,是在当前shell环境下进行的,程序能找到环境变量;而系统自动执行任务调度时,除了默认的环境,是不会加载任何其他环境变量的。因此就需要在crontab文件中指定任务运行所需的所有环境变量,或者在程序中使用绝对路径。
(1)在jar包同级目录创建配置文件conf.properties并写入配置数据:
confData=data
//from www.fhadmin.cn
public class Test{
public String getData() throws IOException {
//获取jar包同级目录
String path = this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
String[] pathSplit = path.split("/");
String jarName = pathSplit[pathSplit.length - 1];
String jarPath = path.replace(jarName, "");
String pathName=jarPath+"minhang.properties";
System.out.println("配置文件路径:"+jarPath);
//读取配置文件
Properties properties = new Properties();
File file = new File(pathName);
FileInputStream fis = new FileInputStream(file);
properties.load(fis);
fis.close();
//获取配置文件数据
String confData = properties.getProperty("confData");
System.out.println(confData);
}
}
(3)执行jar包
java -jar jarNanexxx
到此这篇关于SpringBoot 运行 jar 包读取外部配置文件的文章就介绍到这了,更多相关springboot 配置文件内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: springboot 运行 jar 包读取外部配置文件的问题
本文链接: https://www.lsjlt.com/news/131049.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0