java中获取项目路径

Posted 龙宇在天

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java中获取项目路径相关的知识,希望对你有一定的参考价值。

代码:

package com.demo;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.io.File;
 
 
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
@RestController
public class Application {
     
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
     
    @GetMapping("/lujing")
    public void getLujing() throws Exception{
        //当前项目下路径
        File file = new File("");
        String filePath = file.getCanonicalPath();
        System.out.println(filePath);
 
        //当前项目下xml文件夹
        File file1 = new File("");
        String filePath1 = file1.getCanonicalPath()+File.separator+"xml\";
        System.out.println(filePath1);
 
        //获取类加载的根路径
        File file3 = new File(this.getClass().getResource("/").getPath());
        System.out.println(file3);
 
        //获取当前类的所在工程路径
        File file4 = new File(this.getClass().getResource("").getPath());
        System.out.println(file4);
 
        //获取所有的类路径 包括jar包的路径
        System.out.println(System.getProperty("java.class.path"));
    }
}

测试结果

1.当前项目下路径E:Workexample
 
2.当前项目下xml文件夹E:Workexamplexml
 
3.获取类加载的根路径E:Workexampleoutproductionclasses
 
4.获取当前类的所在工程路径E:Workexampleoutproductionclassescomdemo
 
5.获取所有的类路径 包括jar包的路径D:Javajdk1.8.0_65jrelibcharsets.jar;D:Javajdk1.8.0_65jrelibdeploy.jar;D:Javajdk1.8.0_65jrelibextaccess-bridge-64.jar;D:Javajdk1.8.0_65jrelibextcldrdata.jar;D:Javajdk1.8.0_65jrelibextdnsns.jar;D:Javajdk1.8.0_65jrelibextjaccess.jar;D:Javajdk1.8.0_65jrelibextjfxrt.jar;D:Javajdk1.8.0_65jrelibextlocaledata.jar;D:Javajdk1.8.0_65jrelibext
ashorn.jar;D:Javajdk1.8.0_65jrelibextsunec.jar;D:Javajdk1.8.0_65jrelibextsunjce_provider.jar;D:Javajdk1.8.0_65jrelibextsunmscapi.jar;D:Javajdk1.8.0_65jrelibextsunpkcs11.jar;D:Javajdk1.8.0_65jrelibextzipfs.jar;D:Javajdk1.8.0_65jrelibjavaws.jar;D:Javajdk1.8.0_65jrelibjce.jar;D:Javajdk1.8.0_65jrelibjfr.jar;D:Javajdk1.8.0_65jrelibjfxswt.jar;D:Javajdk1.8.0_65jrelibjsse.jar;D:Javajdk1.8.0_65jrelibmanagement-agent.jar;D:Javajdk1.8.0_65jrelibplugin.jar;D:Javajdk1.8.0_65jrelib
esources.jar;D:Javajdk1.8.0_65jrelib
t.jar;E:Workexampleoutproductionclasses;C:UsersLIX.gradlecachesmodules-2files-2.1org.springframework.bootspring-boot-starter-actuator1.5.8.RELEASE927c40a1af518dc240dfdf7aaf0a9673c3a127c5spring-boot-starter-actuator-1.5.8.RELEASE.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.springframework.bootspring-boot-starter-thymeleaf1.5.9.RELEASEf1a07e590d7742b7c57367ba23a3c0eecb491d48spring-boot-starter-thymeleaf-1.5.9.RELEASE.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.springframework.bootspring-boot-starter-web1.5.9.RELEASEda9733d5e6ba6d1a97deead1738713dc0631b3espring-boot-starter-web-1.5.9.RELEASE.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.springframework.bootspring-boot-starter-data-jpa1.5.9.RELEASE8628320876aa12cc406216523effe954fbd354c0spring-boot-starter-data-jpa-1.5.9.RELEASE.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.springframework.bootspring-boot-starter-test1.5.9.RELEASE5adf6fbf53be00b7ae52f657d564122edbe5156spring-boot-starter-test-1.5.9.RELEASE.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.springframework.bootspring-boot-actuator1.5.8.RELEASEaac530fe877ed91c92831019755c49f29b08e6spring-boot-actuator-1.5.8.RELEASE.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.springframeworkspring-webmvc4.3.13.RELEASE87a095c7a1d13fe433ae6712787238c1dbaa6919spring-webmvc-4.3.13.RELEASE.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.springframeworkspring-web4.3.13.RELEASE7cd084992d546165ede3e99bc31ee49c937f0ce7spring-web-4.3.13.RELEASE.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.springframework.dataspring-data-jpa1.11.9.RELEASE64c80f12361d5b74cf98a3433b59a5c961d78d38spring-data-jpa-1.11.9.RELEASE.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.springframeworkspring-test4.3.13.RELEASEf4ff71b0d628d37f334d4f1f197c1b8cea364d01spring-test-4.3.13.RELEASE.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.springframework.bootspring-boot-starter-aop1.5.9.RELEASEfab419df8d7a13968a51ebfcbcad7ef320d66184spring-boot-starter-aop-1.5.9.RELEASE.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.springframework.bootspring-boot-test-autoconfigure1.5.9.RELEASE428495e25b0531b2a977f51c5d29011c817b8eespring-boot-test-autoconfigure-1.5.9.RELEASE.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.springframework.bootspring-boot-test1.5.9.RELEASE7e604d56325f04a8b89a280e647746fc69e5a21fspring-boot-test-1.5.9.RELEASE.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.springframework.bootspring-boot-starter-jdbc1.5.9.RELEASE6769ecea55e952afb64f17975638c5afd1e59ef3spring-boot-starter-jdbc-1.5.9.RELEASE.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.springframework.bootspring-boot-starter1.5.9.RELEASE6bfb34db2715ee86c6e2337fa145650588b4b1b3spring-boot-starter-1.5.9.RELEASE.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.springframework.bootspring-boot-autoconfigure1.5.9.RELEASE88dd18bdaf317ae40971573a051fd47265f2ed39spring-boot-autoconfigure-1.5.9.RELEASE.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.springframework.bootspring-boot1.5.9.RELEASEf472cdf948288796fb31e0bb03f6e7b022b763a0spring-boot-1.5.9.RELEASE.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.springframeworkspring-context4.3.13.RELEASE6ab1c189a13182fa3256f6bbc68fafdaf8f5a19espring-context-4.3.13.RELEASE.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.springframeworkspring-aop4.3.13.RELEASE61f427b55951f59b50f3c2bd0de3c48960e2a342spring-aop-4.3.13.RELEASE.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.springframeworkspring-orm4.3.13.RELEASE382d3ce044204947f8f2b3fa67a665d4e2bd346fspring-orm-4.3.13.RELEASE.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.springframeworkspring-jdbc4.3.13.RELEASE68e95c2a065641559c7dab7b605bc5847ab026adspring-jdbc-4.3.13.RELEASE.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.springframework.dataspring-data-commons1.13.9.RELEASE3910a598235d2e9c1ca56f34c5e62bb5ce23778spring-data-commons-1.13.9.RELEASE.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.springframeworkspring-tx4.3.13.RELEASE6ebf7b3f2099fa884038d9d7bd44817ebd9c5b4aspring-tx-4.3.13.RELEASE.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.springframeworkspring-beans4.3.13.RELEASE8613a7077722f1d97f8cb3fea4b0b40bfadf4496spring-beans-4.3.13.RELEASE.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.springframeworkspring-expression4.3.13.RELEASEda22bf7df1262e429b49653be6cd5b89b6affe5espring-expression-4.3.13.RELEASE.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.springframeworkspring-core4.3.13.RELEASEeea18d7f4d01f1baa1b6728b678b5a6fe23c61f6spring-core-4.3.13.RELEASE.jar;C:UsersLIX.gradlecachesmodules-2files-2.1commons-loggingcommons-logging1.24bfc12adfe4842bf07b657f0369c4cb522955686commons-logging-1.2.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.apache.logging.log4jlog4j-api2.78de00e382a817981b737be84cb8def687d392963log4j-api-2.7.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.springframework.bootspring-boot-starter-tomcat1.5.9.RELEASE4615056f8a2bcad03ba8cdf410419ee3d101c5bbspring-boot-starter-tomcat-1.5.9.RELEASE.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.hibernatehibernate-validator5.3.6.Final4c730be8bf55a8e9a61e79a2f2c079abf48b718hibernate-validator-5.3.6.Final.jar;C:UsersLIX.gradlecachesmodules-2files-2.1com.fasterxml.jackson.corejackson-databind2.8.10f7b83cb2bc4b88d53961e749e1ad32f49ef017b7jackson-databind-2.8.10.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.hibernatehibernate-entitymanager5.0.12.Final302a526f5058290e9cbd719a5caf9f248d344719hibernate-entitymanager-5.0.12.Final.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.hibernatehibernate-core5.0.12.Finale58bf1c660e6706d8e2cbb53bae110f574366102hibernate-core-5.0.12.Final.jar;C:UsersLIX.gradlecachesmodules-2files-2.1javax.transactionjavax.transaction-api1.2d81aff979d603edd90dcd8db2abc1f4ce6479e3ejavax.transaction-api-1.2.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.springframeworkspring-aspects4.3.13.RELEASEc91788e6b7c431379d0f7fe5f85f427d7e3f01aespring-aspects-4.3.13.RELEASE.jar;C:UsersLIX.gradlecachesmodules-2files-2.1com.jayway.jsonpathjson-path2.2.022290d17944bd239fabf5ac69005a60a7ecbbbcbjson-path-2.2.0.jar;C:UsersLIX.gradlecachesmodules-2files-2.1junitjunit4.122973d150c0dc1fefe998f834810d68f278ea58ecjunit-4.12.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.assertjassertj-core2.6.0532c3fc4f66bcfee4989a3514f1cd56203a33adassertj-core-2.6.0.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.mockitomockito-core1.10.19e8546f5bef4e061d8dd73895b4e8f40e3fe6effemockito-core-1.10.19.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.hamcresthamcrest-library1.34785a3c21320980282f9f33d0d1264a69040538fhamcrest-library-1.3.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.hamcresthamcrest-core1.342a25dc3219429f0e5d060061f71acb49bf010a0hamcrest-core-1.3.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.skyscreamerjsonassert1.4.09cdbb373a06f6513e51e8c545ee6a5e981463edbjsonassert-1.4.0.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.thymeleaf	hymeleaf-spring42.1.6.RELEASEa18784dab5e9337a53ce3213b205f927217849f2	hymeleaf-spring4-2.1.6.RELEASE.jar;C:UsersLIX.gradlecachesmodules-2files-2.1
z.net.ultraq.thymeleaf	hymeleaf-layout-dialect1.4.08d7810c069ed1534b9631fb1e85c35973546086	hymeleaf-layout-dialect-1.4.0.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.apache.tomcat.embed	omcat-embed-websocket8.5.2352f07abcae10dc7e1764304b0877def175c2c833	omcat-embed-websocket-8.5.23.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.apache.tomcat.embed	omcat-embed-core8.5.2379261793a47f507890ee08f749b9d81774e4f7f0	omcat-embed-core-8.5.23.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.apache.tomcat.embed	omcat-embed-el8.5.2398d979cde444dffa6d434c8377d0123b2dfa614c	omcat-embed-el-8.5.23.jar;C:UsersLIX.gradlecachesmodules-2files-2.1javax.validationvalidation-api1.1.0.Final8613ae82954779d518631e05daa73a6a954817d5validation-api-1.1.0.Final.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.hibernate.commonhibernate-commons-annotations5.0.1.Final71e1cff3fcb20d3b3af4f3363c3ddb24d33c6879hibernate-commons-annotations-5.0.1.Final.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.jboss.loggingjboss-logging3.3.0.Final3616bb87707910296e2c195dc016287080bba5afjboss-logging-3.3.0.Final.jar;C:UsersLIX.gradlecachesmodules-2files-2.1com.fasterxmlclassmate1.3.12ad2fd09dcf5607ca96f8ef432096a96986c40aclassmate-1.3.1.jar;C:UsersLIX.gradlecachesmodules-2files-2.1com.fasterxml.jackson.corejackson-annotations2.8.045b426f7796b741035581a176744d91090e2e6fbjackson-annotations-2.8.0.jar;C:UsersLIX.gradlecachesmodules-2files-2.1com.fasterxml.jackson.corejackson-core2.8.10eb21a035c66ad307e66ec8fce37f5d50fd62d039jackson-core-2.8.10.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.aspectjaspectjweaver1.8.13ad94df2a28d658a40dc27bbaff6a1ce5fbf04e9baspectjweaver-1.8.13.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.apache.tomcat	omcat-jdbc8.5.236036400a8f2d90dd3ae3a4d09e5136c56002887b	omcat-jdbc-8.5.23.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.hibernate.javax.persistencehibernate-jpa-2.1-api1.0.0.Final5e731d961297e5a07290bfaf3db1fbc8bbbf405ahibernate-jpa-2.1-api-1.0.0.Final.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.thymeleaf	hymeleaf2.1.6.RELEASEc362b4ecbeddc86a0cb7e767a88ce27e9c8147e6	hymeleaf-2.1.6.RELEASE.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.javassistjavassist3.18.1-GAd9a09f7732226af26bf99f19e2cffe0ae219db5bjavassist-3.18.1-GA.jar;C:UsersLIX.gradlecachesmodules-2files-2.1antlrantlr2.7.783cd2cd674a217ade95a4bb83a8a14f351f48bd0antlr-2.7.7.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.jbossjandex2.0.0.Final3e899258936f94649c777193e1be846387ed54b3jandex-2.0.0.Final.jar;C:UsersLIX.gradlecachesmodules-2files-2.1dom4jdom4j1.6.15d3ccc056b6f056dbf0dddfdf43894b9065a8f94dom4j-1.6.1.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.springframework.bootspring-boot-starter-logging1.5.9.RELEASE30fd62a1ce9639759d90d49608501082314c1628spring-boot-starter-logging-1.5.9.RELEASE.jar;C:UsersLIX.gradlecachesmodules-2files-2.1ch.qos.logbacklogback-classic1.1.11ccedfbacef4a6515d2983e3f89ed753d5d4fb665logback-classic-1.1.11.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.slf4jjcl-over-slf4j1.7.25f8c32b13ff142a513eeb5b6330b1588dcb2c0461jcl-over-slf4j-1.7.25.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.slf4jjul-to-slf4j1.7.25af5364cd6679bfffb114f0dec8a157aaa283b76jul-to-slf4j-1.7.25.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.slf4jlog4j-over-slf4j1.7.25a87bb47468f47ee7aabbd54f93e133d4215769c3log4j-over-slf4j-1.7.25.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.slf4jslf4j-api1.7.25da76ca59f6a57ee3102f8f9bd9cee742973efa8aslf4j-api-1.7.25.jar;C:UsersLIX.gradlecachesmodules-2files-2.1
et.minidevjson-smart2.2.15b9e5df7a62d1279b70dc882b041d249c4f0b002json-smart-2.2.1.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.objenesisobjenesis2.187c0ea803b69252868d09308b4618f766f135a96objenesis-2.1.jar;C:UsersLIX.gradlecachesmodules-2files-2.1com.vaadin.external.googleandroid-json.0.20131108.vaadin1fa26d351fe62a6a17f5cda1287c1c6110dec413fandroid-json-0.0.20131108.vaadin1.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.codehaus.groovygroovy2.4.39c8664435dd00c5316a344eea4b86dc46601d416groovy-2.4.3.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.apache.tomcat	omcat-annotations-api8.5.23aaf17df9fe0240e9e9d5375d24d5f177174b73d9	omcat-annotations-api-8.5.23.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.apache.tomcat	omcat-juli8.5.2398e7f0610b7b2fb8303f11be0210e3f5a56a7d55	omcat-juli-8.5.23.jar;C:UsersLIX.gradlecachesmodules-2files-2.1xml-apisxml-apis1.0.b23136ca936f64c9d68529f048c2618bd356bf85c9xml-apis-1.0.b2.jar;C:UsersLIX.gradlecachesmodules-2files-2.1
et.minidevaccessors-smart1.1a527213f2fea112a04c9bdf0ec0264e34104cd08accessors-smart-1.1.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.ow2.asmasm5.0.3dcc2193db20e19e1feca8b1240dbbc4e190824faasm-5.0.3.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.yamlsnakeyaml1.177a27ea250c5130b2922b86dea63cbb1cc10a660csnakeyaml-1.17.jar;C:UsersLIX.gradlecachesmodules-2files-2.1ch.qos.logbacklogback-core1.1.1188b8df40340eed549fb07e2613879bf6b006704dlogback-core-1.1.11.jar;C:UsersLIX.gradlecachesmodules-2files-2.1ognlognl3.0.837e1aebfde7eb7baebc9ad4f85116ef9009c5fc5ognl-3.0.8.jar;C:UsersLIX.gradlecachesmodules-2files-2.1org.unbescapeunbescape1.1.0.RELEASEab0db4fe0a6fa89fb8da2a40008a4e63a7f3f5b9unbescape-1.1.0.RELEASE.jar;D:IntelliJ IDEA 2017.2.6libidea_rt.jar

以上是关于java中获取项目路径的主要内容,如果未能解决你的问题,请参考以下文章

java中获取项目路径

java项目根目录和类路径问题

通过java获取当前项目路径

如何在java中获取当前项目的路径

在 Android 片段中获取 Java.Lang.NullPointerException

java项目中文件的路径