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