Getting the current working directory helps you manage your files, resources, etc., efficiently. When it comes to Java, getting the current working directory means getting the path of the folder from which your program is running. It’s a common task, and there are different ways to do it in Java.
This Java guide illustrates several built-in and third-party methods to get the current working directory in Java.
How to Get/Obtain Current Working Directory (CWD) in Java
To get/obtain the current working directory in Java, you can utilize the classes like “System”, “Paths”, “File”, “FileSystems”, or “SystemUtils”. These Java classes offer different methods that help us get the current working directory quickly and smoothly. So, let’s get started with the most convenient, widely used, and user-friendly method.
Method 1: System Class
The “System” is a core class of the “java.lang” package that offers various useful methods and fields. The “getProperty()” is one such method that retrieves the current system properties. You can use this method with the “user.dir” property to retrieve the current working directory(CWD):
public class GetCurrentDirectory {
public static void main(String[] args) {
String currentDir = System.getProperty("user.dir");
System.out.println("The Current Working Directory is ==> " + currentDir);
}
}
The “user.dir” points to the current working directory (where Java starts and executes the current program):
Method 2: Paths Class
The “Paths” class belongs to the “java.nio.file” package. It offers various static methods to convert the path string or URL to a path. You can import the Paths class from the corresponding package and execute the following code to get the current working directory(CWD) in Java:
import java.nio.file.Paths;
public class GetCurrentDirectory {
public static void main(String[] args) {
String cwd = Paths.get("").toAbsolutePath().toString();
System.out.println("Current Working Directory is ==> " + cwd);
}
}
Here,
- First, we utilize the get() method with an empty string to get the current relative path.
- This path is then converted into an absolute path via the “toAbsolutePath()” method.
- In addition to this, we utilize the toString() method to convert the absolute path to a string.
- Finally, we print this converted path/string on the console via the println() method.
Method 3: File Class
In Java, the “File” class is associated with Java’s “io” package and is used to work with the files. It offers a very handy method named “getAbsolutePath()”. It retrieves the absolute path of the file, as demonstrated in the below code snippet:
import java.io.File;
public class GetCurrentDirectory {
public static void main(String[] args) {
String cwd = new File("").getAbsolutePath();
System.out.println("Current Working Directory is ==> " + cwd);
}
}
In this code, we execute the “getAbsolutePath()” method along with the “File()” method to get the current working directory in Java:
Method 4: FileSystems Class
The “FileSystems” belongs to the “java.nio.file” package. It provides numerous methods to perform different file operations effectively, such as retrieving the current working directory. To do this, first, import the essential classes like “FileSystems” and “Path” from the “java.nio.file” package:
import java.nio.file.FileSystems;
import java.nio.file.Path;
public class GetCurrentDirectory { public static void main(String[] args) {
Path currentDir = FileSystems.getDefault().getPath("");
String currentDirName = currentDir.toAbsolutePath().toString();
System.out.println("Current Working Directory is ==> " + currentDirName);
}
}
In the main() method,
- We use the “getDefault().getPath(“”);” returns the path of the root directory of the default file system.
- After this, we use the “toAbsolutePath().toString()” to convert the retrieved path to an absolute path and then its respective string representation.
- Finally, we display the current working directory on the console:
Method 5: SystemUtils Class
SystemUtils is one of the utility classes of the “Apache Commons Lang”. This class provides different methods to determine the system properties. To use this class, first, add the below-stated dependency into the auto-created “pom.xml” file of your Maven project:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.14.0</version>
</dependency>
Now let’s import the desired class from the Apache Commons via the following statement:
import org.apache.commons.lang3.SystemUtils;
public class GetWorkingDirectory {
public static void main(String[] args) {
String currentDirName = SystemUtils.getUserDir().getAbsolutePath();
System.out.println("Current Working Directory is ==> " + currentDirName); }
}
In the main() method, we use the “getUserDir().getAbsolutePath()” method of the SystemUtils class to get the current working directory:
How to Get Java Home Directory
Invoke the getProperty() method and pass the “java.home” system property as its argument:
public class GetCurrentDirectory {
public static void main(String[] args) {
String homeDir = System.getProperty("java.home");
System.out.println("The Java Home Directory is ==> " + homeDir);
}
}
On successful execution of the stated method, you will get the output something like this:
How to Get the Size of Current Working Directory in Java
To get the size of the current working directory in Java, invoke/use the length() method on the current working directory. It retrieves/shows the size of the current working directory in bytes:
public class GetCurrentDirectory {
public static void main(String[] args) {
String cwd = System.getProperty("user.dir");
System.out.println("The CWD is ==> " + cwd);
System.out.println("The Total Size of CWD is ==> " + cwd.length() + " bytes");
}
}
In this code, first, we get the current directory using “System.getProperty()”. After this, we invoke the length() method on the retrieved current directory. As a result, we get the current directory’s size on the console as follows:
That’s all about getting the current directory in Java.
Conclusion
To get the current working directory in Java, you can use classes like “System”, “Paths”, “File”, “FileSystems” and “SystemUtils”. The most convenient among them is the “getProperty()” method of Java’s System class. This method is executed with a system property “user.dir”, representing the directory in which JVM was invoked. All these methods are discussed in this write-up along with appropriate examples that will help you understand this concept effortlessly.