Showing posts with label Path. Show all posts

How to get current working directory path in Java?

Following excerpt show how to get current working directory path in Java using java.nio.file.Path (Java 7 or up).

Source code (CurrentWorkingDirectory.java)
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * Get current working directory path.
 * @author javaQuery
 * @date 27th December, 2016
 * @Github: https://github.com/javaquery/Examples
 */
public class CurrentWorkingDirectory {

    public static void main(String[] args) {
        Path path = Paths.get("");
        String currentWorkingDirectory = path.toAbsolutePath().toString();
        System.out.println("Current working directory: " + currentWorkingDirectory);
    }
}

Output
Current working directory: C:\Users\Vicky\Documents\NetBeansProjects\Examples