SpringBoot获取运行Jar包的目录
private String getJarFilePath() {
ApplicationHome home = new ApplicationHome(getClass());
File jarFile = home.getSource();
return jarFile.getParentFile().toString();
}
实际应用实例:
private String getReportFile(){
Calendar date = Calendar.getInstance();
if(!StringUtils.hasText(rootDir)){
rootDir = "D://Zhyb//Pdfs";
}
if( rootDir.indexOf(":") < 0 ){
rootDir = getJarFilePath() + rootDir;
}
File file = new File(rootDir + File.separator + date.get(Calendar.YEAR)
+ File.separator + (date.get(Calendar.MONTH)+1) + File.separator
+ date.get(Calendar.DAY_OF_MONTH));
if(!file.exists()){
file.mkdirs();
}
return file.getPath();
}
private String getJarFilePath() {
ApplicationHome home = new ApplicationHome(getClass());
File jarFile = home.getSource();
return jarFile.getParentFile().toString();
}