Algorithm


  1. Import Necessary Packages:

    • Import the necessary Java packages to access the required classes.
  2. Get Current Working Directory:

    • Use the System.getProperty("user.dir") method to retrieve the current working directory.
  3. Display the Current Working Directory:

    • Print or display the obtained current working directory to the console or any desired output.

 

Code Examples

#1 Code Example- Get current working directory

Code - Java Programming

public class CurrDirectory {

    public static void main(String[] args) {

        String path = System.getProperty("user.dir");
        
        System.out.println("Working Directory = " + path);

    }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Working Directory = C:\Users\Admin\Desktop\currDir

#2 Code Example- Get the current working directory using Path

Code - Java Programming

import java.nio.file.Paths;

public class CurrDirectory {

    public static void main(String[] args) {

        String path = Paths.get("").toAbsolutePath().toString();
        System.out.println("Working Directory = " + path);

    }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Working Directory = C:\Users\Admin\Desktop\currDir
Advertisements

Demonstration


Java Programing Example to Get Current Working Directory-DevsEnv