You can get the current working directory in Java using the System.getProperty("user.dir")
method. Here's how you can do it:
java
public class CurrentWorkingDirectoryExample {
public static void main(String[] args) {
String currentWorkingDir = System.getProperty("user.dir");
System.out.println("Current Working Directory: " + currentWorkingDir);
}
}
In this example, the System.getProperty("user.dir")
method retrieves the system property for the current working directory. The returned value is a string representing the absolute path of the current working directory.
When you run the program, it will display the current working directory on your system. Keep in mind that the current working directory might vary depending on where you execute the Java program from, such as the location of the compiled class or the directory from which you run the java
command.
Comments
Post a Comment