---
title: "java获取项目路径"
author: "Janne"
date: "2020-09-07T10:13:49Z"
tags: []
categories: ["编程语言"]
slug: "java"
draft: false
lastmod: "2020-09-07T10:13:49Z"
id: 2516657
url: "https://janne.iteye.com/blog/2516657"
---

SpringBoot项目jar发布获取jar包所在目录路径
参考：https://blog.csdn.net/liangcha007/article/details/88526181
//第一种
        File path0 = new File(ResourceUtils.getURL("classpath:").getPath());
        if(!path0.exists()) path0 = new File("");
        System.out.println("第1="+path0.getAbsolutePath());
        //E:\code\project\XX\webserviceswagger\target\classes

        //第二种
        System.out.println("第2="+System.getProperty("user.dir"));
        //E:\code\project\XX\webserviceswagger

        //第三种
        String path1 = ClassUtils.getDefaultClassLoader().getResource("").getPath();
        System.out.println("第3="+ URLDecoder.decode(path1, "utf-8"));
        ///E:/code/project/XX/webserviceswagger/target/classes/

        //第四种
        String path2 = ResourceUtils.getURL("classpath:").getPath();
        System.out.println("第4="+path2);
        ///E:/code/project/XX/webserviceswagger/target/classes/

        //第五种
        ApplicationHome h = new ApplicationHome(getClass());
        File jarF = h.getSource();
        System.out.println("第5="+jarF.getParentFile().toString());
        //E:\code\project\XX\webserviceswagger\target