Java Utililty Methods Jar File Find

List of utility methods to do Jar File Find

Description

The list of methods to do Jar File Find are organized into topic(s).

Method

StringgetJarLocation(Class clazz)
This will return the directory location of the executable jar.
String executableLocation;
try {
    URI uri = clazz.getProtectionDomain().getCodeSource().getLocation().toURI();
    if (uri.toString().endsWith(".jar")) {
        return new File(uri.getPath()).getParentFile().getPath() + "/";
    return uri.getPath();
} catch (URISyntaxException e) {
...
URLgetJarLocation(final Class cClass)
Return url of jar we are executing
return cClass.getProtectionDomain().getCodeSource().getLocation();
ManifestgetJarManifest(File file)
get Jar Manifest
JarFile jar = null;
try {
    jar = new JarFile(file);
    return jar.getManifest();
} finally {
    if (jar != null) {
        jar.close();
StringgetJarOrDir(Class mainClass)
Get JAR file or directory from where the given class is loaded.
String jar = mainClass.getProtectionDomain().getCodeSource().getLocation().getPath();
try {
    jar = URLDecoder.decode(jar, "UTF-8");
} catch (UnsupportedEncodingException e) {
if (jar.startsWith("/")) {
    jar = jar.substring(1);
return jar;
StringgetJarPath(Class caller)
get Jar Path
try {
    URL url = caller.getProtectionDomain().getCodeSource().getLocation();
    String path = URLDecoder.decode(url.getFile(), "UTF-8");
    path = path.substring(1, path.lastIndexOf('/')); 
    if (path.endsWith("target/classes")) 
        path = path.substring(0, path.length() - "/classes".length());
    path = path.replace("/", File.separator);
    return path + File.separator;
...
URLgetJarPath(String pkg)
get Jar Path
try {
    Enumeration<URL> resources = Thread.currentThread().getContextClassLoader()
            .getResources(pkg.replace('.', '/'));
    while (resources.hasMoreElements()) {
        URL url = (URL) resources.nextElement();
        return url;
    return null;
...
StringgetJarPathOf(Class classObject)
Returns the path to the jar-file the specified class object resides in.
try {
    String path = URLDecoder
            .decode(classObject.getProtectionDomain().getCodeSource().getLocation().getPath(), "UTF-8");
    return path;
} catch (UnsupportedEncodingException e) {
return "";