I had to mess around a lot before I finally found a working (and short) solution.
It is possible that the jarLocation
comes with a prefix like file:\
or jar:file\
, which can be removed by using String#substring()
.
URL jarLocationUrl = MyClass.class.getProtectionDomain().getCodeSource().getLocation();
String jarLocation = new File(jarLocationUrl.toString()).getParent();