Get working dir path in Java 20

Find this useful? Support us: Star on GitHub 6
Category: File | Language: Java 20

To get the current working directory path in Java 20, you can use the System.getProperty("user.dir") method call, which returns the system property named "user.dir". This property represents the current working directory as a string.

Here is an example code snippet that demonstrates how to obtain the current working directory path in Java 20:

public class WorkingDirPathExample {
    public static void main(String[] args) {
        String workingDirPath = System.getProperty("user.dir");
        System.out.println("Current working directory: " + workingDirPath);
    }
}

When you run this program, the output will be the current working directory path as a string. For example, if you run this program from the command line with the java command, the output might look something like this:

Current working directory: /Users/username/Documents/workspace/myproject

Note that the working directory path might be different on different systems, and may also depend on how and where the Java program is executed.