编程语言
首页 > 编程语言> > Java 获取 jar包所在目录

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,String,jar,getPath,public,substring,Dcm4che3demoApplication,path,目录
来源: https://blog.csdn.net/wang_jing_jing/article/details/119565979