javaandroid-studiogetpropertyintellij-15

Inconsistency with System.getProperty("user.dir")


I have been using this line of code in AndroidStudio to get the path of a file in my project:

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

Currently when I am trying to access the same project using intelliJ the value of srcDir returned is different. Any solutions to this?

Went through SO-722003, but that doesn't resolve my issue. Can't use SO-12413952 since the project location may differ on different system after the user's directory.


Solution

  • getProperty("user.dir") returns the current working directory. when you start java from command line, it is the directory from which you ran the command. You can use a script or a batch file to set the current working directory to where your project is located.

    When you run or debug your project from IntelliJ, it is the directory specified as "Working Directory" in your Run/Debug configuration. See the screenshot below

    IntelliJ Run/Debug configuration

    You can set it to be the same as the location of your project.