Java Jar File Find getJarPathOf(Class classObject)

Here you can find the source of getJarPathOf(Class classObject)

Description

Returns the path to the jar-file the specified class object resides in.

License

Open Source License

Parameter

Parameter Description
classObject The class object to check

Return

The jar-file the specified class object resides in

Declaration

public static String getJarPathOf(Class<?> classObject) 

Method Source Code

//package com.java2s;
/*/*from   ww w . jav  a2 s.  c o  m*/
 * Copyright (C) 2016 Sebastian Hjelm
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 */

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

public class Main {
    /**
     * Returns the path to the jar-file the specified class object resides in. This
     *  only works if the specified class is within a jar-file.
     * @param classObject The class object to check
     * @return The jar-file the specified class object resides in
     */
    public static String getJarPathOf(Class<?> classObject) {
        try {
            String path = URLDecoder
                    .decode(classObject.getProtectionDomain().getCodeSource().getLocation().getPath(), "UTF-8");
            return path;
        } catch (UnsupportedEncodingException e) {
        }

        return "";
    }
}

Related

  1. getJarLocation(final Class cClass)
  2. getJarManifest(File file)
  3. getJarOrDir(Class mainClass)
  4. getJarPath(Class caller)
  5. getJarPath(String pkg)