更新时间:2023-02-21 15:25:14
public class JavaApplication {
public static void main(String[] args) {
System.out.println("Working Directory = " + System.getProperty("user.dir"));
}
}
这将打印从中初始化应用程序的完整绝对路径.
从文档:>
java.io
程序包使用当前用户目录解析相对路径名.当前目录表示为系统属性,即user.dir
,是从其中调用JVM的目录.
I want to access my current working directory using java.
My code :
String current = new java.io.File( "." ).getCanonicalPath();
System.out.println("Current dir:"+current);
String currentDir = System.getProperty("user.dir");
System.out.println("Current dir using System:" +currentDir);
Output:
Current dir: C:\WINDOWS\system32
Current dir using System: C:\WINDOWS\system32
My output is not correct because C drive is not my current directory.
How to get the current directory ?
public class JavaApplication {
public static void main(String[] args) {
System.out.println("Working Directory = " + System.getProperty("user.dir"));
}
}
This will print a complete absolute path from where your application was initialized.
From the documentation:
java.io
package resolve relative pathnames using current user directory. The current directory is represented as system property, that is, user.dir
and is the directory from where the JVM was invoked.