Java获取项目路径的方法汇总(依据可查)
依据
在Java程序中想得到当前项目的路径, 那么一定要有一个资源知道或者拥有这个路径, 只要知道了这个资源, 那么路径我们怎么都可以拿得到
- 工具IDEA
- 项目根路径 E:/java/Study/javaSE/base/out/production/base/
拥有项目路径的资源
1. 根路径在哪
以IDEA为例
2. 知道根路径的资源
第一, 我自己创建的类肯定知道根路径
// /E:/java/Study/javaSE/base/out/production/base/
// System.out.println(FlowDemo1.class.getResource("/").getPath());
// /E:/java/Study/javaSE/base/out/production/base/twice/flow/
// System.out.println(FlowDemo1.class.getResource("").getPath());
第二, 我自己创建的各种资源肯定知道
// 空
// System.out.println(new File("").getPath());
// E:javaStudyjavaSEase
// System.out.println(new File("").getAbsolutePath());
// E:javaStudyjavaSEase
// System.out.println(new File("").getCanonicalPath());
第三, 加载我创建的类的类加载器肯定也知道
// file:/E:/java/Study/javaSE/base/out/production/base/
// System.out.println(FlowDemo1.class.getClassLoader().getResource(""));
// null
// System.out.println(FlowDemo1.class.getClassLoader().getResource("/"));
第四, 已有的类试试看是否知道
// file:/E:/java/Study/javaSE/base/out/production/base/
// System.out.println(Class.class.getResource("/"));
// System.out.println(String.class.getResource("/"));
// null
// System.out.println(Class.class.getResource(""));
// System.out.println(String.class.getResource(""));
从这看出, 已有的类也是知道项目的根路径
第五, 系统配置应该知道全局的和局部的
局部的有
// E:javaStudyjavaSEase
// System.out.println(System.getProperty("user.dir"));
// System.out.println(System.getProperty("java.class.path"));
全部有
{java.runtime.name=Java(TM) SE Runtime Environment, sun.boot.library.path=F:JavaDevToolJDKjdkjrein, java.vm.version=25.221-b11, 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=, java.vm.specification.name=Java Virtual Machine Specification, user.dir=E:javaStudyjavaSEase, java.runtime.version=1.8.0_221-b11, java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment, java.endorsed.dirs=F:JavaDevToolJDKjdkjrelibendorsed, os.arch=amd64, java.io.tmpdir=C:UsersxxAppDataLocalTemp\, line.separator=
, java.vm.specification.vendor=Oracle Corporation, user.variant=, os.name=Windows 10, sun.jnu.encoding=GBK, java.library.path=F:JavaDevToolJDKjdkin;C:WINDOWSSunJavain;C:WINDOWSsystem32;C:WINDOWS;F:JavaLinuxxmanagerXlpd 6;F:JavaLinuxxmanagerXftp 6;F:JavaLinuxxmanagerXshell 6;F:JavaLinuxxmanagerXmanager 6;C:Program Files (x86)Common FilesOracleJavajavapath;C:WINDOWSsystem32;C:WINDOWS;C:WINDOWSSystem32Wbem;C:WINDOWSSystem32WindowsPowerShellv1.0;C:WINDOWSSystem32OpenSSH;C:Program Filesdotnet;F:JavaExtendToolgitGitcmd;F:ToolSoftwareMicrosoft VS Codein;C:Program FilesMicrosoft SQL Server120ToolsBinn;C:Program FilesCommon FilesAutodesk Shared;F:BIMGisBimNode;C:Program FilesMySQLMySQL Shell 8.0in;F:JavaDevToolJDKjdkin;F:JavaDevToolJDKjdkjrein;C:Program FilesBandizip;C:UsersxxAppDataRoaming
pm;., java.specification.name=Java Platform API Specification, java.class.version=52.0, sun.management.compiler=HotSpot 64-Bit Tiered Compilers, os.version=10.0, user.home=C:Usersxx, user.timezone=, java.awt.printerjob=sun.awt.windows.WPrinterJob, file.encoding=UTF-8, java.specification.version=1.8, java.class.path=F:JavaDevToolJDKjdkjrelibcharsets.jar;F:JavaDevToolJDKjdkjrelibdeploy.jar;F:JavaDevToolJDKjdkjrelibextaccess-bridge-64.jar;F:JavaDevToolJDKjdkjrelibextcldrdata.jar;F:JavaDevToolJDKjdkjrelibextdnsns.jar;F:JavaDevToolJDKjdkjrelibextjaccess.jar;F:JavaDevToolJDKjdkjrelibextjfxrt.jar;F:JavaDevToolJDKjdkjrelibextlocaledata.jar;F:JavaDevToolJDKjdkjrelibext
ashorn.jar;F:JavaDevToolJDKjdkjrelibextsunec.jar;F:JavaDevToolJDKjdkjrelibextsunjce_provider.jar;F:JavaDevToolJDKjdkjrelibextsunmscapi.jar;F:JavaDevToolJDKjdkjrelibextsunpkcs11.jar;F:JavaDevToolJDKjdkjrelibextzipfs.jar;F:JavaDevToolJDKjdkjrelibjavaws.jar;F:JavaDevToolJDKjdkjrelibjce.jar;F:JavaDevToolJDKjdkjrelibjfr.jar;F:JavaDevToolJDKjdkjrelibjfxswt.jar;F:JavaDevToolJDKjdkjrelibjsse.jar;F:JavaDevToolJDKjdkjrelibmanagement-agent.jar;F:JavaDevToolJDKjdkjrelibplugin.jar;F:JavaDevToolJDKjdkjrelib
esources.jar;F:JavaDevToolJDKjdkjrelib
t.jar;E:javaStudyjavaSEaseoutproductionase;F:JavaDevToolideaIntelliJ IDEA 2019.3.2libidea_rt.jar, user.name=xx, java.vm.specification.version=1.8, sun.java.command=twice.flow.FlowDemo1, java.home=F:JavaDevToolJDKjdkjre, 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_221, java.ext.dirs=F:JavaDevToolJDKjdkjrelibext;C:WINDOWSSunJavalibext, sun.boot.class.path=F:JavaDevToolJDKjdkjrelib
esources.jar;F:JavaDevToolJDKjdkjrelib
t.jar;F:JavaDevToolJDKjdkjrelibsunrsasign.jar;F:JavaDevToolJDKjdkjrelibjsse.jar;F:JavaDevToolJDKjdkjrelibjce.jar;F:JavaDevToolJDKjdkjrelibcharsets.jar;F:JavaDevToolJDKjdkjrelibjfr.jar;F:JavaDevToolJDKjdkjreclasses, 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}
总结
只要找到拥有根路径的资源, 那么一定可以从中获取项目路径