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:\Work\example 2.当前项目下xml文件夹E:\Work\example\xml 3.获取类加载的根路径E:\Work\example\out\production\classes 4.获取当前类的所在工程路径E:\Work\example\out\production\classes\com\demo 5.获取所有的类路径 包括jar包的路径D:\Java\jdk1.8.0_65\jre\lib\charsets.jar;D:\Java\jdk1.8.0_65\jre\lib\deploy.jar;D:\Java\jdk1.8.0_65\jre\lib\ext\access-bridge-64.jar;D:\Java\jdk1.8.0_65\jre\lib\ext\cldrdata.jar;D:\Java\jdk1.8.0_65\jre\lib\ext\dnsns.jar;D:\Java\jdk1.8.0_65\jre\lib\ext\jaccess.jar;D:\Java\jdk1.8.0_65\jre\lib\ext\jfxrt.jar;D:\Java\jdk1.8.0_65\jre\lib\ext\localedata.jar;D:\Java\jdk1.8.0_65\jre\lib\ext\nashorn.jar;D:\Java\jdk1.8.0_65\jre\lib\ext\sunec.jar;D:\Java\jdk1.8.0_65\jre\lib\ext\sunjce_provider.jar;D:\Java\jdk1.8.0_65\jre\lib\ext\sunmscapi.jar;D:\Java\jdk1.8.0_65\jre\lib\ext\sunpkcs11.jar;D:\Java\jdk1.8.0_65\jre\lib\ext\zipfs.jar;D:\Java\jdk1.8.0_65\jre\lib\javaws.jar;D:\Java\jdk1.8.0_65\jre\lib\jce.jar;D:\Java\jdk1.8.0_65\jre\lib\jfr.jar;D:\Java\jdk1.8.0_65\jre\lib\jfxswt.jar;D:\Java\jdk1.8.0_65\jre\lib\jsse.jar;D:\Java\jdk1.8.0_65\jre\lib\management-agent.jar;D:\Java\jdk1.8.0_65\jre\lib\plugin.jar;D:\Java\jdk1.8.0_65\jre\lib\resources.jar;D:\Java\jdk1.8.0_65\jre\lib\rt.jar;E:\Work\example\out\production\classes;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-actuator\1.5.8.RELEASE\927c40a1af518dc240dfdf7aaf0a9673c3a127c5\spring-boot-starter-actuator-1.5.8.RELEASE.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-thymeleaf\1.5.9.RELEASE\f1a07e590d7742b7c57367ba23a3c0eecb491d48\spring-boot-starter-thymeleaf-1.5.9.RELEASE.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-web\1.5.9.RELEASE\bda9733d5e6ba6d1a97deead1738713dc0631b3e\spring-boot-starter-web-1.5.9.RELEASE.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-data-jpa\1.5.9.RELEASE\8628320876aa12cc406216523effe954fbd354c0\spring-boot-starter-data-jpa-1.5.9.RELEASE.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-test\1.5.9.RELEASE\b5adf6fbf53be00b7ae52f657d564122edbe5156\spring-boot-starter-test-1.5.9.RELEASE.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-actuator\1.5.8.RELEASE\aac530fe877ed91c92831019755c49f29b08e6\spring-boot-actuator-1.5.8.RELEASE.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.springframework\spring-webmvc\4.3.13.RELEASE\87a095c7a1d13fe433ae6712787238c1dbaa6919\spring-webmvc-4.3.13.RELEASE.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.springframework\spring-web\4.3.13.RELEASE\7cd084992d546165ede3e99bc31ee49c937f0ce7\spring-web-4.3.13.RELEASE.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.springframework.data\spring-data-jpa\1.11.9.RELEASE\64c80f12361d5b74cf98a3433b59a5c961d78d38\spring-data-jpa-1.11.9.RELEASE.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.springframework\spring-test\4.3.13.RELEASE\f4ff71b0d628d37f334d4f1f197c1b8cea364d01\spring-test-4.3.13.RELEASE.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-aop\1.5.9.RELEASE\fab419df8d7a13968a51ebfcbcad7ef320d66184\spring-boot-starter-aop-1.5.9.RELEASE.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-test-autoconfigure\1.5.9.RELEASE\b428495e25b0531b2a977f51c5d29011c817b8ee\spring-boot-test-autoconfigure-1.5.9.RELEASE.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-test\1.5.9.RELEASE\7e604d56325f04a8b89a280e647746fc69e5a21f\spring-boot-test-1.5.9.RELEASE.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-jdbc\1.5.9.RELEASE\6769ecea55e952afb64f17975638c5afd1e59ef3\spring-boot-starter-jdbc-1.5.9.RELEASE.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter\1.5.9.RELEASE\6bfb34db2715ee86c6e2337fa145650588b4b1b3\spring-boot-starter-1.5.9.RELEASE.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-autoconfigure\1.5.9.RELEASE\88dd18bdaf317ae40971573a051fd47265f2ed39\spring-boot-autoconfigure-1.5.9.RELEASE.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot\1.5.9.RELEASE\f472cdf948288796fb31e0bb03f6e7b022b763a0\spring-boot-1.5.9.RELEASE.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.springframework\spring-context\4.3.13.RELEASE\6ab1c189a13182fa3256f6bbc68fafdaf8f5a19e\spring-context-4.3.13.RELEASE.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.springframework\spring-aop\4.3.13.RELEASE\61f427b55951f59b50f3c2bd0de3c48960e2a342\spring-aop-4.3.13.RELEASE.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.springframework\spring-orm\4.3.13.RELEASE\382d3ce044204947f8f2b3fa67a665d4e2bd346f\spring-orm-4.3.13.RELEASE.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.springframework\spring-jdbc\4.3.13.RELEASE\68e95c2a065641559c7dab7b605bc5847ab026ad\spring-jdbc-4.3.13.RELEASE.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.springframework.data\spring-data-commons\1.13.9.RELEASE\3910a598235d2e9c1ca56f34c5e62bb5ce23778\spring-data-commons-1.13.9.RELEASE.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.springframework\spring-tx\4.3.13.RELEASE\6ebf7b3f2099fa884038d9d7bd44817ebd9c5b4a\spring-tx-4.3.13.RELEASE.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.springframework\spring-beans\4.3.13.RELEASE\8613a7077722f1d97f8cb3fea4b0b40bfadf4496\spring-beans-4.3.13.RELEASE.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.springframework\spring-expression\4.3.13.RELEASE\da22bf7df1262e429b49653be6cd5b89b6affe5e\spring-expression-4.3.13.RELEASE.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.springframework\spring-core\4.3.13.RELEASE\eea18d7f4d01f1baa1b6728b678b5a6fe23c61f6\spring-core-4.3.13.RELEASE.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\commons-logging\commons-logging\1.2\4bfc12adfe4842bf07b657f0369c4cb522955686\commons-logging-1.2.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.apache.logging.log4j\log4j-api\2.7\8de00e382a817981b737be84cb8def687d392963\log4j-api-2.7.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-tomcat\1.5.9.RELEASE\4615056f8a2bcad03ba8cdf410419ee3d101c5bb\spring-boot-starter-tomcat-1.5.9.RELEASE.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.hibernate\hibernate-validator\5.3.6.Final\4c730be8bf55a8e9a61e79a2f2c079abf48b718\hibernate-validator-5.3.6.Final.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.core\jackson-databind\2.8.10\f7b83cb2bc4b88d53961e749e1ad32f49ef017b7\jackson-databind-2.8.10.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.hibernate\hibernate-entitymanager\5.0.12.Final\302a526f5058290e9cbd719a5caf9f248d344719\hibernate-entitymanager-5.0.12.Final.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.hibernate\hibernate-core\5.0.12.Final\e58bf1c660e6706d8e2cbb53bae110f574366102\hibernate-core-5.0.12.Final.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\javax.transaction\javax.transaction-api\1.2\d81aff979d603edd90dcd8db2abc1f4ce6479e3e\javax.transaction-api-1.2.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.springframework\spring-aspects\4.3.13.RELEASE\c91788e6b7c431379d0f7fe5f85f427d7e3f01ae\spring-aspects-4.3.13.RELEASE.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\com.jayway.jsonpath\json-path\2.2.0\22290d17944bd239fabf5ac69005a60a7ecbbbcb\json-path-2.2.0.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\junit\junit\4.12\2973d150c0dc1fefe998f834810d68f278ea58ec\junit-4.12.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.assertj\assertj-core\2.6.0\b532c3fc4f66bcfee4989a3514f1cd56203a33ad\assertj-core-2.6.0.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.mockito\mockito-core\1.10.19\e8546f5bef4e061d8dd73895b4e8f40e3fe6effe\mockito-core-1.10.19.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.hamcrest\hamcrest-library\1.3\4785a3c21320980282f9f33d0d1264a69040538f\hamcrest-library-1.3.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.hamcrest\hamcrest-core\1.3\42a25dc3219429f0e5d060061f71acb49bf010a0\hamcrest-core-1.3.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.skyscreamer\jsonassert\1.4.0\9cdbb373a06f6513e51e8c545ee6a5e981463edb\jsonassert-1.4.0.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.thymeleaf\thymeleaf-spring4\2.1.6.RELEASE\a18784dab5e9337a53ce3213b205f927217849f2\thymeleaf-spring4-2.1.6.RELEASE.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\nz.net.ultraq.thymeleaf\thymeleaf-layout-dialect\1.4.0\8d7810c069ed1534b9631fb1e85c35973546086\thymeleaf-layout-dialect-1.4.0.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.apache.tomcat.embed\tomcat-embed-websocket\8.5.23\52f07abcae10dc7e1764304b0877def175c2c833\tomcat-embed-websocket-8.5.23.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.apache.tomcat.embed\tomcat-embed-core\8.5.23\79261793a47f507890ee08f749b9d81774e4f7f0\tomcat-embed-core-8.5.23.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.apache.tomcat.embed\tomcat-embed-el\8.5.23\98d979cde444dffa6d434c8377d0123b2dfa614c\tomcat-embed-el-8.5.23.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\javax.validation\validation-api\1.1.0.Final\8613ae82954779d518631e05daa73a6a954817d5\validation-api-1.1.0.Final.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.hibernate.common\hibernate-commons-annotations\5.0.1.Final\71e1cff3fcb20d3b3af4f3363c3ddb24d33c6879\hibernate-commons-annotations-5.0.1.Final.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.jboss.logging\jboss-logging\3.3.0.Final\3616bb87707910296e2c195dc016287080bba5af\jboss-logging-3.3.0.Final.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\com.fasterxml\classmate\1.3.1\2ad2fd09dcf5607ca96f8ef432096a96986c40a\classmate-1.3.1.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.core\jackson-annotations\2.8.0\45b426f7796b741035581a176744d91090e2e6fb\jackson-annotations-2.8.0.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.core\jackson-core\2.8.10\eb21a035c66ad307e66ec8fce37f5d50fd62d039\jackson-core-2.8.10.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.aspectj\aspectjweaver\1.8.13\ad94df2a28d658a40dc27bbaff6a1ce5fbf04e9b\aspectjweaver-1.8.13.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.apache.tomcat\tomcat-jdbc\8.5.23\6036400a8f2d90dd3ae3a4d09e5136c56002887b\tomcat-jdbc-8.5.23.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.hibernate.javax.persistence\hibernate-jpa-2.1-api\1.0.0.Final\5e731d961297e5a07290bfaf3db1fbc8bbbf405a\hibernate-jpa-2.1-api-1.0.0.Final.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.thymeleaf\thymeleaf\2.1.6.RELEASE\c362b4ecbeddc86a0cb7e767a88ce27e9c8147e6\thymeleaf-2.1.6.RELEASE.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.javassist\javassist\3.18.1-GA\d9a09f7732226af26bf99f19e2cffe0ae219db5b\javassist-3.18.1-GA.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\antlr\antlr\2.7.7\83cd2cd674a217ade95a4bb83a8a14f351f48bd0\antlr-2.7.7.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.jboss\jandex\2.0.0.Final\3e899258936f94649c777193e1be846387ed54b3\jandex-2.0.0.Final.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\dom4j\dom4j\1.6.1\5d3ccc056b6f056dbf0dddfdf43894b9065a8f94\dom4j-1.6.1.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-logging\1.5.9.RELEASE\30fd62a1ce9639759d90d49608501082314c1628\spring-boot-starter-logging-1.5.9.RELEASE.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\ch.qos.logback\logback-classic\1.1.11\ccedfbacef4a6515d2983e3f89ed753d5d4fb665\logback-classic-1.1.11.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.slf4j\jcl-over-slf4j\1.7.25\f8c32b13ff142a513eeb5b6330b1588dcb2c0461\jcl-over-slf4j-1.7.25.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.slf4j\jul-to-slf4j\1.7.25\af5364cd6679bfffb114f0dec8a157aaa283b76\jul-to-slf4j-1.7.25.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.slf4j\log4j-over-slf4j\1.7.25\a87bb47468f47ee7aabbd54f93e133d4215769c3\log4j-over-slf4j-1.7.25.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.slf4j\slf4j-api\1.7.25\da76ca59f6a57ee3102f8f9bd9cee742973efa8a\slf4j-api-1.7.25.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\net.minidev\json-smart\2.2.1\5b9e5df7a62d1279b70dc882b041d249c4f0b002\json-smart-2.2.1.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.objenesis\objenesis\2.1\87c0ea803b69252868d09308b4618f766f135a96\objenesis-2.1.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\com.vaadin.external.google\android-json\0.0.20131108.vaadin1\fa26d351fe62a6a17f5cda1287c1c6110dec413f\android-json-0.0.20131108.vaadin1.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.codehaus.groovy\groovy\2.4.3\9c8664435dd00c5316a344eea4b86dc46601d416\groovy-2.4.3.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.apache.tomcat\tomcat-annotations-api\8.5.23\aaf17df9fe0240e9e9d5375d24d5f177174b73d9\tomcat-annotations-api-8.5.23.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.apache.tomcat\tomcat-juli\8.5.23\98e7f0610b7b2fb8303f11be0210e3f5a56a7d55\tomcat-juli-8.5.23.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\xml-apis\xml-apis\1.0.b2\3136ca936f64c9d68529f048c2618bd356bf85c9\xml-apis-1.0.b2.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\net.minidev\accessors-smart\1.1\a527213f2fea112a04c9bdf0ec0264e34104cd08\accessors-smart-1.1.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.ow2.asm\asm\5.0.3\dcc2193db20e19e1feca8b1240dbbc4e190824fa\asm-5.0.3.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.yaml\snakeyaml\1.17\7a27ea250c5130b2922b86dea63cbb1cc10a660c\snakeyaml-1.17.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\ch.qos.logback\logback-core\1.1.11\88b8df40340eed549fb07e2613879bf6b006704d\logback-core-1.1.11.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\ognl\ognl\3.0.8\37e1aebfde7eb7baebc9ad4f85116ef9009c5fc5\ognl-3.0.8.jar;C:\Users\LIX\.gradle\caches\modules-2\files-2.1\org.unbescape\unbescape\1.1.0.RELEASE\ab0db4fe0a6fa89fb8da2a40008a4e63a7f3f5b9\unbescape-1.1.0.RELEASE.jar;D:\IntelliJ IDEA 2017.2.6\lib\idea_rt.jar