AndreaNobili
AndreaNobili

Reputation: 42957

How can I obtain the parent of the path inside an URL object in Java?

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

Answers (2)

M A
M A

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

Guy Bouallet
Guy Bouallet

Reputation: 2125

Use the getParentFile method from standard Java.

Upvotes: 1

Related Questions