How to get the path of a running JAR file?

return new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath());

 

Obviously, this will do odd things if your class was loaded from a non-file location.
String path = Test.class.getProtectionDomain().getCodeSource().getLocation().getPath();

String decodedPath = URLDecoder.decode(path, "UTF-8");

This should solve the problem with spaces and special characters.