How to get the path of a running JAR file? How to get the path of a running JAR file? java java

How to get the path of a running JAR file?


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

Replace "MyClass" with the name of your class.

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


Best solution for me:

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.


To obtain the File for a given Class, there are two steps:

  1. Convert the Class to a URL
  2. Convert the URL to a File

It is important to understand both steps, and not conflate them.

Once you have the File, you can call getParentFile to get the containing folder, if that is what you need.

Step 1: Class to URL

As discussed in other answers, there are two major ways to find a URL relevant to a Class.

  1. URL url = Bar.class.getProtectionDomain().getCodeSource().getLocation();

  2. URL url = Bar.class.getResource(Bar.class.getSimpleName() + ".class");

Both have pros and cons.

The getProtectionDomain approach yields the base location of the class (e.g., the containing JAR file). However, it is possible that the Java runtime's security policy will throw SecurityException when calling getProtectionDomain(), so if your application needs to run in a variety of environments, it is best to test in all of them.

The getResource approach yields the full URL resource path of the class, from which you will need to perform additional string manipulation. It may be a file: path, but it could also be jar:file: or even something nastier like bundleresource://346.fwk2106232034:4/foo/Bar.class when executing within an OSGi framework. Conversely, the getProtectionDomain approach correctly yields a file: URL even from within OSGi.

Note that both getResource("") and getResource(".") failed in my tests, when the class resided within a JAR file; both invocations returned null. So I recommend the #2 invocation shown above instead, as it seems safer.

Step 2: URL to File

Either way, once you have a URL, the next step is convert to a File. This is its own challenge; see Kohsuke Kawaguchi's blog post about it for full details, but in short, you can use new File(url.toURI()) as long as the URL is completely well-formed.

Lastly, I would highly discourage using URLDecoder. Some characters of the URL, : and / in particular, are not valid URL-encoded characters. From the URLDecoder Javadoc:

It is assumed that all characters in the encoded string are one of the following: "a" through "z", "A" through "Z", "0" through "9", and "-", "_", ".", and "*". The character "%" is allowed but is interpreted as the start of a special escaped sequence.

...

There are two possible ways in which this decoder could deal with illegal strings. It could either leave illegal characters alone or it could throw an IllegalArgumentException. Which approach the decoder takes is left to the implementation.

In practice, URLDecoder generally does not throw IllegalArgumentException as threatened above. And if your file path has spaces encoded as %20, this approach may appear to work. However, if your file path has other non-alphameric characters such as + you will have problems with URLDecoder mangling your file path.

Working code

To achieve these steps, you might have methods like the following:

/** * Gets the base location of the given class. * <p> * If the class is directly on the file system (e.g., * "/path/to/my/package/MyClass.class") then it will return the base directory * (e.g., "file:/path/to"). * </p> * <p> * If the class is within a JAR file (e.g., * "/path/to/my-jar.jar!/my/package/MyClass.class") then it will return the * path to the JAR (e.g., "file:/path/to/my-jar.jar"). * </p> * * @param c The class whose location is desired. * @see FileUtils#urlToFile(URL) to convert the result to a {@link File}. */public static URL getLocation(final Class<?> c) {    if (c == null) return null; // could not load the class    // try the easy way first    try {        final URL codeSourceLocation =            c.getProtectionDomain().getCodeSource().getLocation();        if (codeSourceLocation != null) return codeSourceLocation;    }    catch (final SecurityException e) {        // NB: Cannot access protection domain.    }    catch (final NullPointerException e) {        // NB: Protection domain or code source is null.    }    // NB: The easy way failed, so we try the hard way. We ask for the class    // itself as a resource, then strip the class's path from the URL string,    // leaving the base path.    // get the class's raw resource path    final URL classResource = c.getResource(c.getSimpleName() + ".class");    if (classResource == null) return null; // cannot find class resource    final String url = classResource.toString();    final String suffix = c.getCanonicalName().replace('.', '/') + ".class";    if (!url.endsWith(suffix)) return null; // weird URL    // strip the class's path from the URL string    final String base = url.substring(0, url.length() - suffix.length());    String path = base;    // remove the "jar:" prefix and "!/" suffix, if present    if (path.startsWith("jar:")) path = path.substring(4, path.length() - 2);    try {        return new URL(path);    }    catch (final MalformedURLException e) {        e.printStackTrace();        return null;    }} /** * Converts the given {@link URL} to its corresponding {@link File}. * <p> * This method is similar to calling {@code new File(url.toURI())} except that * it also handles "jar:file:" URLs, returning the path to the JAR file. * </p> *  * @param url The URL to convert. * @return A file path suitable for use with e.g. {@link FileInputStream} * @throws IllegalArgumentException if the URL does not correspond to a file. */public static File urlToFile(final URL url) {    return url == null ? null : urlToFile(url.toString());}/** * Converts the given URL string to its corresponding {@link File}. *  * @param url The URL to convert. * @return A file path suitable for use with e.g. {@link FileInputStream} * @throws IllegalArgumentException if the URL does not correspond to a file. */public static File urlToFile(final String url) {    String path = url;    if (path.startsWith("jar:")) {        // remove "jar:" prefix and "!/" suffix        final int index = path.indexOf("!/");        path = path.substring(4, index);    }    try {        if (PlatformUtils.isWindows() && path.matches("file:[A-Za-z]:.*")) {            path = "file:/" + path.substring(5);        }        return new File(new URL(path).toURI());    }    catch (final MalformedURLException e) {        // NB: URL is not completely well-formed.    }    catch (final URISyntaxException e) {        // NB: URL is not completely well-formed.    }    if (path.startsWith("file:")) {        // pass through the URL as-is, minus "file:" prefix        path = path.substring(5);        return new File(path);    }    throw new IllegalArgumentException("Invalid URL: " + url);}

You can find these methods in the SciJava Common library: