I have a problem with System.getProperty("user.dir")
giving different directory when run by IDE and when I manually compile & run it in cmd. My thing is this, I have project structure like this:
project
- exports
- src
- main
- java
- Main
- file1
- file2
One of the args
in main
method is the name of one of those 2 files, that I then access.
When I configure my run in IDE it works like a charm - the directory I get is C:\Users\**\**\**\project
and it is able to read and write to the file.
But when I compile it in cmd javac Main.java
and then run it, I get C:\Users\**\**\**\project\src\main\java
and because of that, I am unable to access the file without having to modify the path.
My question is, is there like a golden way, that would work for both these cases, without me having to alter the returned path?
EDIT:
For clear understanding, I know what System.getProperty("user.dir")
returns, but my question was, if it is possible to get the same result somehow with using Path
or if I have to get the path and edit it, so that it will end in project directory?
in IDE I get: C:\Users\petri\Desktop\CZM\bicycle-statistics
in cmd: C:\Users\petri\Desktop\CZM\bicycle-statistics\src\main\java
I want to get the same path in cmd, that I got in IDE.
I tried using Paths.get("").toAbsolutePath()
, but it is the same thing.
So, what I did is this:
Path path = Paths.get("").toAbsolutePath();
while (!path.endsWith("project")) {
path = path.getParent();
}
And it works, but I am trying to ask, if there is some more elegant way, because I will have to defend my solution in front of my supervisor.