Reputation: 42957
I have the following problem in a Java application
By this statment I retrieve the current location of my application:
URL location = Main.class.getProtectionDomain().getCodeSource().getLocation();
So for example my location contains a path like: /C:/Projects/edi-sta/out/production/edi-sta/
Ok, how can I easilly obtain the parent path?
For example, considering the previous path, how can I obtain the ****/C:/Projects/edi-sta/out/production/** path (that is the direct parent of the last edi-sta/ folder) ?
Java provide me something that do it out of the box or have I to implement a string manipulation?
Upvotes: 0
Views: 661
Reputation: 72854
You can use URI#resolve(String)
to get a URL
that points to the parent:
URL parentUrl = location.toURI().resolve("..").toURL(); // .. for parent
Upvotes: 2