[英]How to locate the Path of the current project directory in Java (IDE)?
我试图在 Java 中以编程方式定位当前正在运行/调试的项目的路径,我查看了谷歌,发现是System.getProperty("user.id")
,它没有让我找到项目的路径。
我知道 C# 中的命令Environment.currentDirectory
给出了当前运行/调试项目的路径,所以我相信 Java 中也必须有类似的方式。
所以我问是否有人可以告诉我或给我一个代码来如何以编程方式定位当前正在运行/调试的项目的路径?
编辑:我正在编写随 Eclipse 启动加载的插件。 该插件向 eclipse 的工作台添加了一个按钮,在插件的代码中,当我单击该按钮时,我会搜索当前目录路径。
我的目的是,例如当我调试一个项目然后单击按钮时,会弹出一个窗口,显示已调试项目的路径,谢谢
两种方式
System.getProperty("user.dir");
或这个
File currentDirFile = new File(".");
String helper = currentDirFile.getAbsolutePath();
String currentDir = helper.substring(0, helper.length() - currentDirFile.getCanonicalPath().length());//this line may need a try-catch block
这个想法是用“.”获取当前文件夹,然后获取它的绝对位置并从中删除文件名,所以从类似
/home/shark/eclipse/workspace/project/src/com/package/name/bin/Class.class
当你删除 Class.class 你会得到
/home/shark/eclipse/workspace/project/src/com/package/name/bin/
这就是你想要的。
使用user.dir
键使用 java 获取项目位置。
代码:
System.out.println("Present Project Directory : "+ System.getProperty("user.dir"));
输出:
Present Project Directory :C:\\workspace\\MyEclipse 10\\examples\\MySqlMavenDomain
要显示所有系统属性,只需使用System.getProperties();
在System.getProperty("java.version");
使用Key
值System.getProperty("java.version");
获取输出。
代码:
Properties properties = System.getProperties();
Enumeration<Object> enumeration = properties.keys();
for (int i = 0; i < properties.size(); i++) {
Object obj = enumeration.nextElement();
System.out.println("Key: "+obj+"\tOutPut= "+System.getProperty(obj.toString()));
}
输出:
Key: java.runtime.name OutPut= Java(TM) SE Runtime Environment
Key: sun.boot.library.path OutPut= C:\Program Files\Java\jdk1.7.0_45\jre\bin
Key: java.vm.version OutPut= 24.45-b08
Key: java.vm.vendor OutPut= Oracle Corporation
Key: java.vendor.url OutPut= http://java.oracle.com/
Key: path.separator OutPut= ;
Key: java.vm.name OutPut= Java HotSpot(TM) Client VM
Key: file.encoding.pkg OutPut= sun.io
Key: user.country OutPut= US
Key: user.script OutPut=
Key: sun.java.launcher OutPut= SUN_STANDARD
Key: sun.os.patch.level OutPut= Service Pack 3
Key: java.vm.specification.name OutPut= Java Virtual Machine Specification
Key: user.dir OutPut= C:\workspace\MyEclipse 10\examples\MySqlMavenDomain
Key: java.runtime.version OutPut= 1.7.0_45-b18
Key: java.awt.graphicsenv OutPut= sun.awt.Win32GraphicsEnvironment
Key: java.endorsed.dirs OutPut= C:\Program Files\Java\jdk1.7.0_45\jre\lib\endorsed
Key: os.arch OutPut= x86
Key: java.io.tmpdir OutPut= C:\DOCUME~1\UDAYP~2.PBS\LOCALS~1\Temp\
Key: line.separator OutPut=
Key: java.vm.specification.vendor OutPut= Oracle Corporation
Key: user.variant OutPut=
Key: os.name OutPut= Windows XP
Key: sun.jnu.encoding OutPut= Cp1252
Key: java.library.path OutPut= C:\Program Files\Java\jdk1.7.0_45\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files/MyEclipse/Common/binary/com.sun.java.jdk.win32.x86_1.6.0.013/jre/bin/client;C:/Program Files/MyEclipse/Common/binary/com.sun.java.jdk.win32.x86_1.6.0.013/jre/bin;C:/Program Files/MyEclipse/Common/binary/com.sun.java.jdk.win32.x86_1.6.0.013/jre/lib/i386;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\system32\WindowsPowerShell\v1.0;D:\Program Files\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\;D:\Program Files\Microsoft SQL Server\100\Tools\Binn\;D:\Program Files\Microsoft SQL Server\100\DTS\Binn\;.
Key: java.specification.name OutPut= Java Platform API Specification
Key: java.class.version OutPut= 51.0
Key: sun.management.compiler OutPut= HotSpot Client Compiler
Key: os.version OutPut= 5.1
Key: user.home OutPut= C:\Documents and Settings\uday.p.PBSYSTEMS
Key: user.timezone OutPut=
Key: java.awt.printerjob OutPut= sun.awt.windows.WPrinterJob
Key: file.encoding OutPut= UTF-8
Key: java.specification.version OutPut= 1.7
Key: java.class.path OutPut= C:\workspace\MyEclipse 10\examples\MySqlMavenDomain\target\test-classes;C:\workspace\MyEclipse 10\examples\MySqlMavenDomain\target\classes;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\jstl\jstl\1.2\jstl-1.2.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\javax\servlet\javax.servlet-api\3.1.0\javax.servlet-api-3.1.0.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\mysql\mysql-connector-java\5.1.28\mysql-connector-java-5.1.28.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\javax\mail\mail\1.4.7\mail-1.4.7.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\javax\activation\activation\1.1\activation-1.1.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\junit\junit\4.11\junit-4.11.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\hibernate\hibernate-core\4.3.0.Final\hibernate-core-4.3.0.Final.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\jboss\logging\jboss-logging\3.1.3.GA\jboss-logging-3.1.3.GA.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\jboss\logging\jboss-logging-annotations\1.2.0.Beta1\jboss-logging-annotations-1.2.0.Beta1.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\jboss\spec\javax\transaction\jboss-transaction-api_1.2_spec\1.0.0.Final\jboss-transaction-api_1.2_spec-1.0.0.Final.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\dom4j\dom4j\1.6.1\dom4j-1.6.1.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\xml-apis\xml-apis\1.0.b2\xml-apis-1.0.b2.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\hibernate\common\hibernate-commons-annotations\4.0.4.Final\hibernate-commons-annotations-4.0.4.Final.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\hibernate\javax\persistence\hibernate-jpa-2.1-api\1.0.0.Final\hibernate-jpa-2.1-api-1.0.0.Final.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\javassist\javassist\3.18.1-GA\javassist-3.18.1-GA.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\antlr\antlr\2.7.7\antlr-2.7.7.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\jboss\jandex\1.1.0.Final\jandex-1.1.0.Final.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\hibernate\hibernate-entitymanager\4.3.0.Final\hibernate-entitymanager-4.3.0.Final.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\commons-dbcp\commons-dbcp\1.4\commons-dbcp-1.4.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\commons-fileupload\commons-fileupload\1.3\commons-fileupload-1.3.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\commons-io\commons-io\2.4\commons-io-2.4.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\commons-net\commons-net\3.3\commons-net-3.3.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\commons-codec\commons-codec\1.9\commons-codec-1.9.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\commons-pool\commons-pool\1.6\commons-pool-1.6.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\commons-collections\commons-collections\3.2.1\commons-collections-3.2.1.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\commons-logging\commons-logging\1.1.3\commons-logging-1.1.3.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\commons-beanutils\commons-beanutils\1.9.0\commons-beanutils-1.9.0.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\apache\commons\commons-lang3\3.1\commons-lang3-3.1.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\log4j\log4j\1.2.16\log4j-1.2.16.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\springframework\spring-orm\3.2.6.RELEASE\spring-orm-3.2.6.RELEASE.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\aopalliance\aopalliance\1.0\aopalliance-1.0.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\springframework\spring-jdbc\3.2.6.RELEASE\spring-jdbc-3.2.6.RELEASE.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\springframework\spring-tx\3.2.6.RELEASE\spring-tx-3.2.6.RELEASE.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\springframework\spring-web\3.2.6.RELEASE\spring-web-3.2.6.RELEASE.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\springframework\spring-aop\3.2.6.RELEASE\spring-aop-3.2.6.RELEASE.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\springframework\spring-webmvc\3.2.6.RELEASE\spring-webmvc-3.2.6.RELEASE.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\springframework\spring-aspects\3.2.6.RELEASE\spring-aspects-3.2.6.RELEASE.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\aspectj\aspectjweaver\1.7.2\aspectjweaver-1.7.2.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\springframework\spring-beans\3.2.6.RELEASE\spring-beans-3.2.6.RELEASE.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\springframework\spring-context\3.2.6.RELEASE\spring-context-3.2.6.RELEASE.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\springframework\spring-context-support\3.2.6.RELEASE\spring-context-support-3.2.6.RELEASE.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\springframework\spring-core\3.2.6.RELEASE\spring-core-3.2.6.RELEASE.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\springframework\spring-expression\3.2.6.RELEASE\spring-expression-3.2.6.RELEASE.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\springframework\spring-instrument\3.2.6.RELEASE\spring-instrument-3.2.6.RELEASE.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\springframework\spring-instrument-tomcat\3.2.6.RELEASE\spring-instrument-tomcat-3.2.6.RELEASE.jar
Key: user.name OutPut= uday.p
Key: java.vm.specification.version OutPut= 1.7
Key: sun.java.command OutPut= com.uk.mysqlmaven.domain.test.UserLoginDetails
Key: java.home OutPut= C:\Program Files\Java\jdk1.7.0_45\jre
Key: sun.arch.data.model OutPut= 32
Key: user.language OutPut= en
Key: java.specification.vendor OutPut= Oracle Corporation
Key: awt.toolkit OutPut= sun.awt.windows.WToolkit
Key: java.vm.info OutPut= mixed mode, sharing
Key: java.version OutPut= 1.7.0_45
Key: java.ext.dirs OutPut= C:\Program Files\Java\jdk1.7.0_45\jre\lib\ext;C:\WINDOWS\Sun\Java\lib\ext
Key: sun.boot.class.path OutPut= C:\Program Files\Java\jdk1.7.0_45\jre\lib\resources.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\rt.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\sunrsasign.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\jce.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.7.0_45\jre\classes
Key: java.vendor OutPut= Oracle Corporation
Key: file.separator OutPut= \
Key: java.vendor.url.bug OutPut= http://bugreport.sun.com/bugreport/
Key: sun.io.unicode.encoding OutPut= UnicodeLittle
Key: sun.cpu.endian OutPut= little
Key: sun.desktop OutPut= windows
Key: sun.cpu.isalist OutPut= pentium_pro+mmx pentium_pro pentium+mmx pentium i486 i386 i86
更多详情请参考 班级系统
这是执行此操作的新方法:
Path root = FileSystems.getDefault().getPath("").toAbsolutePath();
Path filePath = Paths.get(root.toString(),"src", "main", "resources", fileName);
或者甚至更好:
Path root = Paths.get(".").normalize().toAbsolutePath();
但我会更进一步:
public Path getUsersProjectRootDirectory() {
String envRootDir = System.getProperty("user.dir");
Path rootDIr = Paths.get(".").normalize().toAbsolutePath();
if ( rootDir.startsWith(envRootDir) ) {
return rootDir;
} else {
throw new RuntimeException("Root dir not found in user directory.");
}
}
System.getProperty("user.dir")
怎么样? 它将为您提供启动程序的工作目录。
有关 Java 系统属性的概述,请参阅 Java 教程中的系统属性。
Java-Projects 没有一条路径! Java-Projects 有多个路径,即使一个类可以在一个“项目”中的不同类路径中具有多个位置。
因此,如果您有一个位于 JRE/lib 中的calculator.jar 和一个在 CD 上具有相同类的calculator.jar:如果您执行calculator.jar 从CD 中的类,java-vm 将从JRE/库!
这个问题经常出现在喜欢加载部署在项目内部的资源的程序员身上。 在这种情况下,
System.getResource("/likebutton.png")
以为例。
您可以使用System.getProperty("user.dir")
获取当前项目路径
在该方法中,您编写 Key 名称以获取不同的路径,如果您不知道 key,则可以找到System.getProperties()
所有属性使用,此方法返回所有带有 key 的属性。 您可以从中手动找到密钥名称。
并写入System.getProperty("KEY NAME")
并获取您的需求路径。
File currDir = new File(".");
String path = currDir.getAbsolutePath();
System.out.println(path);
这将打印.
在末尾。 要删除,只需将字符串截断一个字符,例如:
File currDir = new File(".");
String path = currDir.getAbsolutePath();
path = path.substring(0, path.length()-1);
System.out.println(path);
这是一个代码片段,用于在 java 中检索当前正在运行的 Web 应用程序项目的路径。
public String getPath() throws UnsupportedEncodingException {
String path = this.getClass().getClassLoader().getResource("").getPath();
String fullPath = URLDecoder.decode(path, "UTF-8");
String pathArr[] = fullPath.split("/WEB-INF/classes/");
System.out.println(fullPath);
System.out.println(pathArr[0]);
fullPath = pathArr[0];
return fullPath;
}
来源: https : //dzone.com/articles/get-current-web-application
我刚用过这个:
System.out.println(System.getenv().get("PWD"));
使用 OpenJDK 11
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.