- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get the Current Working Directory in Java
The method java.lang.System.getProperty() is used to obtain the system property. This system property is specified by the key which is the parameter for the method. To obtain the current working directory, the key used is user.dir.
A program that demonstrates this is given as follows −
Example
public class Demo { public static void main(String[] argv) throws Exception { String currentDirectory = System.getProperty("user.dir"); System.out.println("The current working directory is " + currentDirectory); } }
The output of the above program is as follows −
Output
The current working directory is c:\JavaProgram
Now let us understand the above program.
The current working directory is obtained by using the key user.dir with the method java.lang.System.getProperty(). Then the current working directory is printed. A code snippet that demonstrates this is given as follows −
String currentDirectory = System.getProperty("user.dir"); System.out.println("The current working directory is " + currentDirectory);
Advertisements