Java 获取 jar包所在目录

一、直接贴代码

package com.exampl;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.IOException;

@SpringBootApplication
public class Dcm4che3demoApplication {

    public static void main(String[] args) throws IOException {
        Dcm4che3demoApplication dcm4che3demoApplication = new Dcm4che3demoApplication();

        String path = dcm4che3demoApplication.getPath();
        System.out.println("path===>" + path);
    }


    public String getPath() {
        String path = this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
        if (System.getProperty("os.name").contains("dows")) {
            path = path.substring(1, path.length());
        }
        if (path.contains("jar")) {
            path = path.substring(0, path.lastIndexOf("."));
            return path.substring(0, path.lastIndexOf("/"));
        }
        return path.replace("target/classes/", "");
    }
}

二、查看执行效果:

Java 获取 jar包所在目录

 注:以上内容仅提供参考和交流,请勿用于商业用途,如有侵权联系本人删除! 

上一篇:Java遍历文件夹下所有文件然后按照指定规则重新命名


下一篇:西北乱跑娃 --- bottle