2

I have a Java application which I call from the command prompt like this:

C:\Mydir> C:\dir2\my.exe

When I call this application I would like to retrieve at the same time the path "C:\MyDir", i.e. the active directory from where my exe is called in the prompt and not "C:\dir2\" where the exe is found.

How could I do that in Java?

5
  • 1
    File cwd = new File("."); doesn't work? Commented Mar 20, 2012 at 12:56
  • System.getProperty("user.dir");? Commented Mar 20, 2012 at 12:58
  • 1) That is an EXE, not a Java application. 2) The question 'where is the user directory?' is usually the wrong question. What are you trying to offer the end user by knowing this? Is it about loading application resources or preferences? Commented Mar 20, 2012 at 13:06
  • That exe is an application implemented in Java and built/exported into an exe file. I don't want to user directory; i want the current directory from the command prompt which can be whatever; and the path helps in the functionality of the application; the end user isn't using it actively at all Commented Mar 20, 2012 at 13:11
  • possible duplicate of Getting the Current Working Directory in Java Commented Mar 20, 2012 at 13:29

2 Answers 2

2

I believe you would use

String currentLocation = System.getProperty("user.dir");
Sign up to request clarification or add additional context in comments.

2 Comments

doesn't the "user.dir" property return the path to the user's directory? like "C:\Documents and Settings\user\"? i can be in any other directory in my command prompt; not only the user.dir
@schmimona: no, it doesn't. From javadoc (docs.oracle.com/javase/6/docs/api/java/lang/…): user.dir: User's current working directory. The system property you're thinking of is user.home, which returns the user's home directory.
1
File f = new File("");
System.out.println(f.getAbsolutePath());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.