2

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?

3
  • E:\ is the root directory of E: drive, c:\users\zavargadim\desktop is the desktop directory, they essentially mean the same thing. You could use File(String, String) to construct a directory/path combination if that's what you needed Commented Sep 15, 2015 at 4:56
  • You mean you want like this "c:\users\zavargadim\desktop\" ? Commented Sep 15, 2015 at 4:56
  • 1
    user.dir can have or lack a trailing slash depending on any number of factors (how the user entered the directory, operating system, shell options, whatever). Do not rely on it to have a trailing slash or not, and if you need to use that, instead validate it yourself. Commented Sep 15, 2015 at 4:56

3 Answers 3

4

try this

Paths.get(".").toAbsolutePath().normalize().toString() 
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry should I use your idea like so ? String path = Paths.get(".").toAbsolutePath().normalize().toString()
Excuse me your statement gives also c:\users\zavarghadim\desktop NOT c:\users\zavarghadim\desktop\
3

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() 

5 Comments

Sorry I'm new with java I use your idea but the compiler said :can not find symbol :Paths .Should I import anything else java.io.*; ?
Yes. import java.nio.file.*;
Excuse me when I use your statement gives me this erro : incompatible type getRoot() required String but found Path
I use like so : String currentPath = Paths.get(currentPath).getRoot();
If you want to cast to string, you need write that: String currentPath = Paths.get(currentPath).getRoot().toString();
0

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); 

4 Comments

I used it but it is not the answer
May i know what output you really want
I Have an application and run it in both drivers and desktop .When I run from d:\ I get d:\ result but when running from dekstop i get this result : c:\users\zavargadim\desktop . I want c:\users\zavargadim\desktop\
You can externally add a \ to your String and that can be used anywhere. 'String currentpath = System.getProperty("user.dir");' 'String newCurrentPath = currentpath+"\";'

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.