Search code examples
javapathrelative-path

Get relative path from jar file


I tried to reach a special path in Ubuntu, relative to the current jar file. In Windows it is working without any problem:

String jarPath = Configuration.class.getProtectionDomain().getCodeSource().getLocation().getPath();
File f = new File(jarPath+"/../../configurationFile.xml");

However, in Linux I always get the jar file but I cannot step back two directories to the configurationFile.xml

/some/directory/where/xml/is/located/xyz.jar/configurationFile.xml: Not a directory

However, if I do

pwd /some/directory/where/xml/is/located/xyz.jar/../../

it works without any problems.

What I am doing wrong here?

I cannot figure it out.


Solution

  • Use only directories in your path. After you determined the path to your jar file, extract the path to its directory and use directories only.