golitini
golitini

Reputation: 21

How to get current path?

I'm trying to get current path of system. I use this statement for this purpose :

String currentPath = System.getProperty("user.dir");

When I run this statement For Example from E:\. I get E:\

but when I run from desktop I get C:\users\zavarghadim\desktop. The last slash (\) missed. Why this happen? In both type I need last slash c:\users\zavargadim\desktop\

Can anyone help me to solve this problem?

Upvotes: 2

Views: 10489

Answers (3)

kripindas
kripindas

Reputation: 480

Try this

The currentpath is the root folder of your current java project. It can be retrieved by using System Property Function

String currentpath = System.getProperty("user.dir");
System.out.println("current path is:" + currentpath);

Upvotes: 0

Vladislav Kievski
Vladislav Kievski

Reputation: 1647

It gives you user working directory.

String currentPath = System.getProperty("user.dir");

You need to use getRoot, for getting only root component of this path.

Paths.get(currentPath).getRoot()

Upvotes: 3

Jayanti Lal
Jayanti Lal

Reputation: 1185

try this

Paths.get(".").toAbsolutePath().normalize().toString()

Upvotes: 4

Related Questions