Java获取项目路径方式System.getProperty(“user.dir“)
作者:冷冷清清中的风风火火
Java如何获取项目路径
System.getProperty(“user.dir”) 可以获取当前项目的根目录。
最初以为 user.dir 是需要自己配置的参数,实际上不是,直接执行这行代码就可以获取到当前项目的根路径。
System.getProperty(“key”)该方法到底有多少个key?
System.getProperties()给出了答案,通过System.getProperties()方法可以获取到所有的key值
代码如下
public static void main(String[] args) { File sysFile = new File("C:/Users/Administrator/Desktop","sysFile.properties"); Properties propInfo = System.getProperties(); try { propInfo .store(new FileOutputStream(sysFile), "Properties ClASS"); } catch (FileNotFoundException e1) { e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
运行结果
#Properties ClASS
#Mon May 23 17:43:31 CST 2022
java.runtime.name=Java(TM) SE Runtime Environment
sun.boot.library.path=E\:\\dvlp\\Java\\jdk1.8.0_144\\jre\\bin
java.vm.version=25.144-b01
java.vm.vendor=Oracle Corporation
java.vendor.url=http\://java.oracle.com/
path.separator=;
java.vm.name=Java HotSpot(TM) 64-Bit Server VM
file.encoding.pkg=sun.io
user.country=CN
user.script=
sun.java.launcher=SUN_STANDARD
sun.os.patch.level=Service Pack 1
java.vm.specification.name=Java Virtual Machine Specification
user.dir=F\:\\workspace\\springboot-demo-mp
intellij.debug.agent=true
java.runtime.version=1.8.0_144-b01
java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment
java.endorsed.dirs=E\:\\dvlp\\Java\\jdk1.8.0_144\\jre\\lib\\endorsed
os.arch=amd64
java.io.tmpdir=C\:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\
line.separator=\r\n
java.vm.specification.vendor=Oracle Corporation
user.variant=
os.name=Windows 7
sun.jnu.encoding=GBK
java.library.path=E\:\\dvlp\\Java\\jdk1.8.0_144\\bin;C\:\\Windows\\Sun\\Java\\bin;C\:\\Windows\\system32;C\:\\Windows;G\:\\ideaIU\\jbr\\\\bin;G\:\\ideaIU\\jbr\\\\bin\\server;%HADOOP_HOME%\\bin;C\:\\Windows\\system32;C\:\\Windows;C\:\\Windows\\System32\\Wbem;C\:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C\:\\Program Files (x86)\\ATI Technologies\\ATI.ACE\\Core-Static;%JAVA_HOME%\\bin;%JAVA_HOME%\\jre\\bin;G\:\\soft\\python26;%HIVE_HOME%\\bin;D\:\\Program Files\\Git\\cmd;H\:\\npm\\;E\:\\Java\\jdk8u275-b01\\bin;E\:\\Java\\jdk8u275-b01\\jre\\bin;C\:\\Users\\Administrator\\AppData\\Roaming\\npm;H\:\\soft\\VSCode\\Microsoft VS Code\\bin;.
jboss.modules.system.pkgs=com.intellij.rt
java.specification.name=Java Platform API Specification
java.class.version=52.0
sun.management.compiler=HotSpot 64-Bit Tiered Compilers
os.version=6.1
user.home=C\:\\Users\\Administrator
user.timezone=Asia/Shanghai
java.awt.printerjob=sun.awt.windows.WPrinterJob
file.encoding=UTF-8
java.specification.version=1.8
java.class.path=E\:\\dvlp\\Java\\jdk1.8.0_144\\jre\\lib\\charsets.jar;E\:\\dvlp\\Java\\jdk1.8.0_144\\jre\\lib\\deploy.jar;E\:\\dvlp\\Java\\jdk1.8.0_144\\jre\\lib\\ext\\access-bridge-64.jar;E\:\\dvlp\\Java\\jdk1.8.0_144\\jre\\lib\\ext\\cldrdata.jar;E\:\\dvlp\\Java\\jdk1.8.0_144\\jre\\lib\\ext\\dnsns.jar;E\:\\dvlp\\Java\\jdk1.8.0_144\\jre\\lib\\ext\\jaccess.jar;E\:\\dvlp\\Java\\jdk1.8.0_144\\jre\\lib\\ext\\jfxrt.jar;E\:\\dvlp\\Java\\jdk1.8.0_144\\jre\\lib\\ext\\localedata.jar;E\:\\dvlp\\Java\\jdk1.8.0_144\\jre\\lib\\ext\\nashorn.jar;E\:\\dvlp\\Java\\jdk1.8.0_144\\jre\\lib\\ext\\sunec.jar;E\:\\dvlp\\Java\\jdk1.8.0_144\\jre\\lib\\ext\\sunjce_provider.jar;E\:\\dvlp\\Java\\jdk1.8.0_144\\jre\\lib\\ext\\sunmscapi.jar;E\:\\dvlp\\Java\\jdk1.8.0_144\\jre\\lib\\ext\\sunpkcs11.jar;E\:\\dvlp\\Java\\jdk1.8.0_144\\jre\\lib\\ext\\zipfs.jar;E\:\\dvlp\\Java\\jdk1.8.0_144\\jre\\lib\\javaws.jar;E\:\\dvlp\\Java\\jdk1.8.0_144\\jre\\lib\\jce.jar;E\:\\dvlp\\Java\\jdk1.8.0_144\\jre\\lib\\jfr.jar;E\:\\dvlp\\Java\\jdk1.8.0_144\\jre\\lib\\jfxswt.jar;E\:\\dvlp\\Java\\jdk1.8.0_144\\jre\\lib\\jsse.jar;E\:\\dvlp\\Java\\jdk1.8.0_144\\jre\\lib\\management-agent.jar;E\:\\dvlp\\Java\\jdk1.8.0_144\\jre\\lib\\plugin.jar;E\:\\dvlp\\Java\\jdk1.8.0_144\\jre\\lib\\resources.jar;E\:\\dvlp\\Java\\jdk1.8.0_144\\jre\\lib\\rt.jar;F\:\\workspace\\springboot-demo-mp\\target\\classes;C\:\\Users\\Administrator\\.m2\\repository\\org\\springframework\\boot\\spring-boot-starter-web\\2.3.1.RELEASE\\spring-boot-starter-web-2.3.1.RELEASE.jar;C\:\\Users\\Administrator\\.m2\\repository\\org\\springframework\\boot\\spring-boot-starter\\2.3.1.RELEASE\\spring-boot-starter-2.3.1.RELEASE.jar;C\:\\Users\\Administrator\\.m2\\repository\\org\\springframework\\boot\\spring-boot-starter-logging\\2.3.1.RELEASE\\spring-boot-starter-logging-2.3.1.RELEASE.jar;C\:\\Users\\Administrator\\.m2\\repository\\ch\\qos\\logback\\logback-classic\\1.2.3\\logback-classic-1.2.3.jar;C\:\\Users\\Administrator\\.m2\\repository\\ch\\qos\\logback\\logback-core\\1.2.3\\logback-core-1.2.3.jar;C\:\\Users\\Administrator\\.m2\\repository\\org\\apache\\logging\\log4j\\log4j-to-slf4j\\2.13.3\\log4j-to-slf4j-2.13.3.jar;C\:\\Users\\Administrator\\.m2\\repository\\org\\apache\\logging\\log4j\\log4j-api\\2.13.3\\log4j-api-2.13.3.jar;C\:\\Users\\Administrator\\.m2\\repository\\org\\slf4j\\jul-to-slf4j\\1.7.30\\jul-to-slf4j-1.7.30.jar;C\:\\Users\\Administrator\\.m2\\repository\\jakarta\\annotation\\jakarta.annotation-api\\1.3.5\\jakarta.annotation-api-1.3.5.jar;C\:\\Users\\Administrator\\.m2\\repository\\org\\yaml\\snakeyaml\\1.26\\snakeyaml-1.26.jar;C\:\\Users\\Administrator\\.m2\\repository\\org\\springframework\\boot\\spring-boot-starter-json\\2.3.1.RELEASE\\spring-boot-starter-json-2.3.1.RELEASE.jar;C\:\\Users\\Administrator\\.m2\\repository\\com\\fasterxml\\jackson\\core\\jackson-databind\\2.11.0\\jackson-databind-2.11.0.jar;C\:\\Users\\Administrator\\.m2\\repository\\com\\fasterxml\\jackson\\core\\jackson-annotations\\2.11.0\\jackson-annotations-2.11.0.jar;C\:\\Users\\Administrator\\.m2\\repository\\com\\fasterxml\\jackson\\core\\jackson-core\\2.11.0\\jackson-core-2.11.0.jar;C\:\\Users\\Administrator\\.m2\\repository\\com\\fasterxml\\jackson\\datatype\\jackson-datatype-jdk8\\2.11.0\\jackson-datatype-jdk8-2.11.0.jar;C\:\\Users\\Administrator\\.m2\\repository\\com\\fasterxml\\jackson\\datatype\\jackson-datatype-jsr310\\2.11.0\\jackson-datatype-jsr310-2.11.0.jar;C\:\\Users\\Administrator\\.m2\\repository\\com\\fasterxml\\jackson\\module\\jackson-module-parameter-names\\2.11.0\\jackson-module-parameter-names-2.11.0.jar;C\:\\Users\\Administrator\\.m2\\repository\\org\\springframework\\boot\\spring-boot-starter-tomcat\\2.3.1.RELEASE\\spring-boot-starter-tomcat-2.3.1.RELEASE.jar;C\:\\Users\\Administrator\\.m2\\repository\\org\\apache\\tomcat\\embed\\tomcat-embed-core\\9.0.36\\tomcat-embed-core-9.0.36.jar;C\:\\Users\\Administrator\\.m2\\repository\\org\\glassfish\\jakarta.el\\3.0.3\\jakarta.el-3.0.3.jar;C\:\\Users\\Administrator\\.m2\\repository\\org\\apache\\tomcat\\embed\\tomcat-embed-websocket\\9.0.36\\tomcat-embed-websocket-9.0.36.jar;C\:\\Users\\Administrator\\.m2\\repository\\org\\springframework\\spring-web\\5.2.7.RELEASE\\spring-web-5.2.7.RELEASE.jar;C\:\\Users\\Administrator\\.m2\\repository\\org\\springframework\\spring-beans\\5.2.7.RELEASE\\spring-beans-5.2.7.RELEASE.jar;C\:\\Users\\Administrator\\.m2\\repository\\org\\springframework\\spring-webmvc\\5.2.7.RELEASE\\spring-webmvc-5.2.7.RELEASE.jar;C\:\\Users\\Administrator\\.m2\\repository\\org\\springframework\\spring-aop\\5.2.7.RELEASE\\spring-aop-5.2.7.RELEASE.jar;C\:\\Users\\Administrator\\.m2\\repository\\org\\springframework\\spring-context\\5.2.7.RELEASE\\spring-context-5.2.7.RELEASE.jar;C\:\\Users\\Administrator\\.m2\\repository\\org\\springframework\\spring-expression\\5.2.7.RELEASE\\spring-expression-5.2.7.RELEASE.jar;C\:\\Users\\Administrator\\.m2\\repository\\org\\hamcrest\\hamcrest\\2.2\\hamcrest-2.2.jar;C\:\\Users\\Administrator\\.m2\\repository\\org\\springframework\\spring-core\\5.2.7.RELEASE\\spring-core-5.2.7.RELEASE.jar;C\:\\Users\\Administrator\\.m2\\repository\\org\\springframework\\spring-jcl\\5.2.7.RELEASE\\spring-jcl-5.2.7.RELEASE.jar;C\:\\Users\\Administrator\\.m2\\repository\\org\\springframework\\boot\\spring-boot-starter-thymeleaf\\2.3.1.RELEASE\\spring-boot-starter-thymeleaf-2.3.1.RELEASE.jar;C\:\\Users\\Administrator\\.m2\\repository\\org\\thymeleaf\\thymeleaf-spring5\\3.0.11.RELEASE\\thymeleaf-spring5-3.0.11.RELEASE.jar;C\:\\Users\\Administrator\\.m2\\repository\\org\\thymeleaf\\thymeleaf\\3.0.11.RELEASE\\thymeleaf-3.0.11.RELEASE.jar;C\:\\Users\\Administrator\\.m2\\repository\\org\\attoparser\\attoparser\\2.0.5.RELEASE\\attoparser-2.0.5.RELEASE.jar;C\:\\Users\\Administrator\\.m2\\repository\\org\\unbescape\\unbescape\\1.1.6.RELEASE\\unbescape-1.1.6.RELEASE.jar;C\:\\Users\\Administrator\\.m2\\repository\\org\\thymeleaf\\extras\\thymeleaf-extras-java8time\\3.0.4.RELEASE\\thymeleaf-extras-java8time-3.0.4.RELEASE.jar;C\:\\Users\\Administrator\\.m2\\repository\\mysql\\mysql-connector-java\\8.0.20\\mysql-connector-java-8.0.20.jar;C\:\\Users\\Administrator\\.m2\\repository\\org\\springframework\\boot\\spring-boot-starter-jdbc\\2.3.1.RELEASE\\spring-boot-starter-jdbc-2.3.1.RELEASE.jar;C\:\\Users\\Administrator\\.m2\\repository\\com\\zaxxer\\HikariCP\\3.4.5\\HikariCP-3.4.5.jar;C\:\\Users\\Administrator\\.m2\\repository\\org\\springframework\\spring-jdbc\\5.2.7.RELEASE\\spring-jdbc-5.2.7.RELEASE.jar;C\:\\Users\\Administrator\\.m2\\repository\\org\\springframework\\spring-tx\\5.2.7.RELEASE\\spring-tx-5.2.7.RELEASE.jar;C\:\\Users\\Administrator\\.m2\\repository\\com\\alibaba\\druid\\1.1.6\\druid-1.1.6.jar;C\:\\Users\\Administrator\\.m2\\repository\\com\\alibaba\\druid-spring-boot-starter\\1.1.10\\druid-spring-boot-starter-1.1.10.jar;C\:\\Users\\Administrator\\.m2\\repository\\org\\slf4j\\slf4j-api\\1.7.30\\slf4j-api-1.7.30.jar;C\:\\Users\\Administrator\\.m2\\repository\\org\\springframework\\boot\\spring-boot-autoconfigure\\2.3.1.RELEASE\\spring-boot-autoconfigure-2.3.1.RELEASE.jar;C\:\\Users\\Administrator\\.m2\\repository\\org\\springframework\\spring-aspects\\5.2.7.RELEASE\\spring-aspects-5.2.7.RELEASE.jar;C\:\\Users\\Administrator\\.m2\\repository\\org\\aspectj\\aspectjweaver\\1.9.5\\aspectjweaver-1.9.5.jar;C\:\\Users\\Administrator\\.m2\\repository\\com\\baomidou\\mybatis-plus-boot-starter\\3.5.1\\mybatis-plus-boot-starter-3.5.1.jar;C\:\\Users\\Administrator\\.m2\\repository\\com\\baomidou\\mybatis-plus\\3.5.1\\mybatis-plus-3.5.1.jar;C\:\\Users\\Administrator\\.m2\\repository\\com\\baomidou\\mybatis-plus-extension\\3.5.1\\mybatis-plus-extension-3.5.1.jar;C\:\\Users\\Administrator\\.m2\\repository\\com\\baomidou\\mybatis-plus-core\\3.5.1\\mybatis-plus-core-3.5.1.jar;C\:\\Users\\Administrator\\.m2\\repository\\com\\baomidou\\mybatis-plus-annotation\\3.5.1\\mybatis-plus-annotation-3.5.1.jar;C\:\\Users\\Administrator\\.m2\\repository\\com\\github\\jsqlparser\\jsqlparser\\4.3\\jsqlparser-4.3.jar;C\:\\Users\\Administrator\\.m2\\repository\\org\\mybatis\\mybatis\\3.5.9\\mybatis-3.5.9.jar;C\:\\Users\\Administrator\\.m2\\repository\\org\\mybatis\\mybatis-spring\\2.0.6\\mybatis-spring-2.0.6.jar;C\:\\Users\\Administrator\\.m2\\repository\\com\\baomidou\\mybatis-plus-generator\\3.4.0\\mybatis-plus-generator-3.4.0.jar;C\:\\Users\\Administrator\\.m2\\repository\\org\\springframework\\boot\\spring-boot-starter-data-redis\\2.3.1.RELEASE\\spring-boot-starter-data-redis-2.3.1.RELEASE.jar;C\:\\Users\\Administrator\\.m2\\repository\\org\\springframework\\data\\spring-data-redis\\2.3.1.RELEASE\\spring-data-redis-2.3.1.RELEASE.jar;C\:\\Users\\Administrator\\.m2\\repository\\org\\springframework\\data\\spring-data-keyvalue\\2.3.1.RELEASE\\spring-data-keyvalue-2.3.1.RELEASE.jar;C\:\\Users\\Administrator\\.m2\\repository\\org\\springframework\\data\\spring-data-commons\\2.3.1.RELEASE\\spring-data-commons-2.3.1.RELEASE.jar;C\:\\Users\\Administrator\\.m2\\repository\\org\\springframework\\spring-oxm\\5.2.7.RELEASE\\spring-oxm-5.2.7.RELEASE.jar;C\:\\Users\\Administrator\\.m2\\repository\\org\\springframework\\spring-context-support\\5.2.7.RELEASE\\spring-context-support-5.2.7.RELEASE.jar;C\:\\Users\\Administrator\\.m2\\repository\\io\\lettuce\\lettuce-core\\5.3.1.RELEASE\\lettuce-core-5.3.1.RELEASE.jar;C\:\\Users\\Administrator\\.m2\\repository\\io\\netty\\netty-common\\4.1.50.Final\\netty-common-4.1.50.Final.jar;C\:\\Users\\Administrator\\.m2\\repository\\io\\netty\\netty-handler\\4.1.50.Final\\netty-handler-4.1.50.Final.jar;C\:\\Users\\Administrator\\.m2\\repository\\io\\netty\\netty-resolver\\4.1.50.Final\\netty-resolver-4.1.50.Final.jar;C\:\\Users\\Administrator\\.m2\\repository\\io\\netty\\netty-buffer\\4.1.50.Final\\netty-buffer-4.1.50.Final.jar;C\:\\Users\\Administrator\\.m2\\repository\\io\\netty\\netty-codec\\4.1.50.Final\\netty-codec-4.1.50.Final.jar;C\:\\Users\\Administrator\\.m2\\repository\\io\\netty\\netty-transport\\4.1.50.Final\\netty-transport-4.1.50.Final.jar;C\:\\Users\\Administrator\\.m2\\repository\\io\\projectreactor\\reactor-core\\3.3.6.RELEASE\\reactor-core-3.3.6.RELEASE.jar;C\:\\Users\\Administrator\\.m2\\repository\\org\\reactivestreams\\reactive-streams\\1.0.3\\reactive-streams-1.0.3.jar;C\:\\Users\\Administrator\\.m2\\repository\\junit\\junit\\4.13\\junit-4.13.jar;C\:\\Users\\Administrator\\.m2\\repository\\org\\hamcrest\\hamcrest-core\\2.2\\hamcrest-core-2.2.jar;C\:\\Users\\Administrator\\.m2\\repository\\org\\springframework\\boot\\spring-boot-test\\2.3.1.RELEASE\\spring-boot-test-2.3.1.RELEASE.jar;C\:\\Users\\Administrator\\.m2\\repository\\org\\springframework\\boot\\spring-boot\\2.3.1.RELEASE\\spring-boot-2.3.1.RELEASE.jar;C\:\\Users\\Administrator\\.m2\\repository\\org\\springframework\\spring-test\\5.2.0.RELEASE\\spring-test-5.2.0.RELEASE.jar;C\:\\Users\\Administrator\\.m2\\repository\\com\\alibaba\\fastjson\\1.2.28\\fastjson-1.2.28.jar;C\:\\Users\\Administrator\\.m2\\repository\\commons-beanutils\\commons-beanutils\\1.9.3\\commons-beanutils-1.9.3.jar;C\:\\Users\\Administrator\\.m2\\repository\\commons-logging\\commons-logging\\1.2\\commons-logging-1.2.jar;C\:\\Users\\Administrator\\.m2\\repository\\commons-collections\\commons-collections\\3.2.2\\commons-collections-3.2.2.jar;C\:\\Users\\Administrator\\.m2\\repository\\org\\apache\\commons\\commons-lang3\\3.10\\commons-lang3-3.10.jar;C\:\\Users\\Administrator\\.m2\\repository\\org\\projectlombok\\lombok\\1.18.16\\lombok-1.18.16.jar;G\:\\ideaIU\\lib\\idea_rt.jar;G\:\\ideaIU\\plugins\\java\\lib\\rt\\debugger-agent.jar
user.name=Administrator
java.vm.specification.version=1.8
sun.java.command=com.zyr.springbootdemo.generator.Generator
java.home=E\:\\dvlp\\Java\\jdk1.8.0_144\\jre
sun.arch.data.model=64
user.language=zh
java.specification.vendor=Oracle Corporation
awt.toolkit=sun.awt.windows.WToolkit
java.vm.info=mixed mode
java.version=1.8.0_144
java.ext.dirs=E\:\\dvlp\\Java\\jdk1.8.0_144\\jre\\lib\\ext;C\:\\Windows\\Sun\\Java\\lib\\ext
sun.boot.class.path=E\:\\dvlp\\Java\\jdk1.8.0_144\\jre\\lib\\resources.jar;E\:\\dvlp\\Java\\jdk1.8.0_144\\jre\\lib\\rt.jar;E\:\\dvlp\\Java\\jdk1.8.0_144\\jre\\lib\\sunrsasign.jar;E\:\\dvlp\\Java\\jdk1.8.0_144\\jre\\lib\\jsse.jar;E\:\\dvlp\\Java\\jdk1.8.0_144\\jre\\lib\\jce.jar;E\:\\dvlp\\Java\\jdk1.8.0_144\\jre\\lib\\charsets.jar;E\:\\dvlp\\Java\\jdk1.8.0_144\\jre\\lib\\jfr.jar;E\:\\dvlp\\Java\\jdk1.8.0_144\\jre\\classes
java.vendor=Oracle Corporation
file.separator=\\
java.vendor.url.bug=http\://bugreport.sun.com/bugreport/
sun.io.unicode.encoding=UnicodeLittle
sun.cpu.endian=little
sun.desktop=windows
sun.cpu.isalist=amd64
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。