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

FilegetJarFile(@Nonnull Class clazz)
get Jar File
File file = getClassFile(clazz);
String path = file.getPath();
final int idx = path.lastIndexOf('!');
if (idx == -1) {
    return null;
String jarFilePath = path.substring(0, idx);
if (jarFilePath.startsWith("file:\\")) {
...
FilegetJarFile(Class clazz)
Obtains the jar file that contains the class in question.
String resName = clazz.getName();
resName = "/" + resName.replace('.', '/') + ".class";
URL classURL = clazz.getResource(resName);
if (classURL == null)
    return null;
String jarHeader = "jar:file:";
String urlString = classURL.toString();
if (!urlString.startsWith(jarHeader))
...
StringgetJarFile(Class clazz)
get Jar File
String result = null;
if (clazz != null) {
    String me = clazz.getName().replace(".", "/") + ".class";
    URL dirURL = clazz.getClassLoader().getResource(me);
    if (dirURL.getProtocol().equals("jar")) {
        result = dirURL.getPath().substring(5, dirURL.getPath().indexOf("!"));
return result;
FilegetJarFile(Class clazz)
Returns null or the jar-file containing the given class.
URL jarUrl = (clazz.getProtectionDomain().getCodeSource() == null ? null
        : clazz.getProtectionDomain().getCodeSource().getLocation());
File jarFile = null;
if (jarUrl != null) {
    jarFile = getFile(jarUrl);
    if (!jarFile.isFile()) {
        jarFile = null;
return jarFile;
JarFilegetJarFile(final ClassLoader loader)
get Jar File
final URL resUrl = loader.getResource(MANIFEST);
if (!resUrl.getProtocol().equals("jar")) {
    throw new RuntimeException("Should run as jar");
final String path = resUrl.getPath();
if (!path.startsWith("file:")) {
    throw new RuntimeException("Unknown jar path: " + path);
final String jarPath = path.substring("file:".length(), path.indexOf('!'));
try {
    return new JarFile(URLDecoder.decode(jarPath, "UTF-8"));
} catch (UnsupportedEncodingException e) {
} catch (IOException e) {
return null;
StringgetJarFile(String classPath)
Returns the full path of the jar file that contains the running class
String dir = ClassLoader.getSystemResource(classPath).toString();
try {
    dir = URLDecoder.decode(dir, "UTF-8");
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
dir = dir.replace('\\', '/');
if (dir.startsWith("jar:"))
...
JarFilegetJarFileByResourceName(Class clazz, String resourceName)
Returns the jar file, where resources are located
final URL jarUrl = clazz.getResource("/" + resourceName);
final JarURLConnection connection = (JarURLConnection) jarUrl.openConnection();
final URL url = connection.getJarFileURL();
final File jarFile = new File(url.getFile());
return new JarFile(jarFile);
FilegetJarFolder(Class clazz)
get Jar Folder
File currentJar = urlToFile(clazz.getProtectionDomain().getCodeSource().getLocation());
return currentJar;
StringgetJarLocation()
Gets the path of the running jar file
String path = Main.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath();
if (path.endsWith(".jar"))
    path = path.substring(0, path.lastIndexOf("/"));
return path;
StringgetJarLocation(Class classFromJar)
get Jar Location
return url2File(classFromJar.getProtectionDomain().getCodeSource().getLocation()).getAbsolutePath();