
How to get the current directory in Java with Example
public class CurrentDirectoryExample {
public static void main(String args[]) {
String current = System.getProperty("user.dir");
System.out.println("Current working directory in Java : " + current);
}
}
public static void main(String args[]) {
String current = System.getProperty("user.dir");
System.out.println("Current working directory in Java : " + current);
}
}
If you run the above program from C:\Test it will print C:\Test as the current working directory
C:\Test> java CurrentWorkingDirectoryExample
Current working directory in Java : C:\Test
Current working directory in Java : C:\Test
If you run it from C:\ then it will print C:\ as the current working directory as shown in the below example
C:\> java -cp ./Test CurrentWorkingDirectoryExample
Current working directory in Java : C:\
Current working directory in Java : C:\
That's all on how to get the current directory in Java. It's easy just to remember the name of the system property "user.dir" which gives a directory from where the java command has been executed.
Some other Java tips you may like
No comments :
Post a Comment