JAVA开发示例:如何获取JAR包所在目录


功能需求

在执行Jar文件时,需要读取当前目录下一些配制文件的信息,所以需要知道当前JAR包所在目录

示例代码

public File getCurrentJarDir() {
        try {
            String path = this.getClass().getProtectionDomain().getCodeSource().getLocation().getFile();
            path = java.net.URLDecoder.decode(path, "UTF-8");
            File file = new File(path);
            if (file.isFile()) {
                return new File(file.getParent());
            } else {
               return file;
            }
        }catch (Exception e){
            throw new RuntimeException(e);
        }
}

参考资料

https://blog.csdn.net/weixin_43710268/article/details/109967547


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM