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?

schmimona
  • 849
  • 3
  • 20
  • 40
  • 1
    `File cwd = new File(".");` doesn't work? – Nim Mar 20 '12 at 12:56
  • `System.getProperty("user.dir");`? – assylias Mar 20 '12 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? – Andrew Thompson Mar 20 '12 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 – schmimona Mar 20 '12 at 13:11
  • possible duplicate of [Getting the Current Working Directory in Java](http://stackoverflow.com/questions/4871051/getting-the-current-working-directory-in-java) – Stephen C Mar 20 '12 at 13:29

2 Answers2

2

I believe you would use

String currentLocation = System.getProperty("user.dir");
Chris Aldrich
  • 1,904
  • 1
  • 22
  • 37
  • 1
    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 Mar 20 '12 at 13:03
  • 1
    @schmimona: no, it doesn't. From javadoc (http://docs.oracle.com/javase/6/docs/api/java/lang/System.html#getProperties()): `user.dir: User's current working directory`. The system property you're thinking of is `user.home`, which returns the user's home directory. – Sean Reilly Mar 20 '12 at 13:20
1
File f = new File("");
System.out.println(f.getAbsolutePath());
Sinisha Mihajlovski
  • 1,811
  • 1
  • 20
  • 34