How to get current working directory in java

In this post, we will see how to get current working directory in java.
There are four ways to do it.

Using property user.dir

We can use System.getProperty(“user.dir”) to get current working directory path.
When I ran above program, I got below output:

Using getAbsolutePath()

We can use file.getAbos
When I ran above program, I got below output:

Using Paths.get() (Java 7+)

You can use Paths.get("") method get current working directory in java.

When I ran above program, I got below output:

Using FileSystems.getDefault() (Java 7+)

You can use FileSystem.getDefault() method get current working directory in java.

When I ran above program, I got below output:

That’s all about how to get current working directory in java.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *