SpringBoot打成Jar包运行,获取运行Jar包的目录

SpringBoot获取运行Jar包的目录

//   linux和windows下通用
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 ){
            // 若维护的不是绝对路径,则获取当前Jar包运行的路径,并在Jar包运行的目录下,创建rootDir初始值的文件路径, 如: /zhyb/pdfs ,最终路径未:   jar包路径/zhyb/pdfs
            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();
    }

    // 获取SpringBoot项目发布后的  Jar包   所在的路径:    linux和windows下通用
    private String getJarFilePath() {
        ApplicationHome home = new ApplicationHome(getClass());
        File jarFile = home.getSource();
        return jarFile.getParentFile().toString();
    }
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值