Thiago Chaves Thiago Chaves - 1 year ago 113
Java Question

How to get the path of a running JAR file?

My code runs inside a JAR file, say foo.jar, and I need to know, in the code, in which folder the running foo.jar is.

So, if foo.jar is in

, I want to get that path no matter what my current working directory is.

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

Obviously, this will do odd things if your class was loaded from a non-file location.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download