简体   繁体   中英

Confusion over current working directory in File’s getAbsolutePath method

The file's getAbsolutePath has the tendency that if a relative path or just the file name is provided in the file constructor, then it will resolve the absolute path by prefixing the current working directory.

The definition of current working directory is the directory where we run our Java Program. In my example, my java program resides inside D:\my-app\src\App.java

When I do a System.out.println(System.getProperty("user.dir"));

Then it should ideally print D:\my-app\src

But it is actually printing D:\my-app

Why is it so?

When you start an application from IntelliJ, its working directory is set according to what's set in "Working directory" in the "Run/Debug Configurations" dialog .

There, you can set the working directory to a fixed value such as D:\my-app\src . Another option is to use a path variable . The list of variables that are available apparently can depend on many things - open the dialog and see what's available. One option that I see is $FileDir$ which I imagine would be the directory of the source code file.

I think I found the solution. Intellij was actually causing the issue by implicitly setting the src folder as the source folder. I found it by going to the module setting within Intellij. When I ran the same from a command prompt, the correct working directory ie D:\my-app\src\ is selected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM