Java Jar File Find getJarFile(final ClassLoader loader)

Here you can find the source of getJarFile(final ClassLoader loader)

Description

get Jar File

License

Apache License

Declaration

public static JarFile getJarFile(final ClassLoader loader) 

Method Source Code


//package com.java2s;
/*//from   w w w  . j ava2s  .co m
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.IOException;

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

import java.util.jar.JarFile;

public class Main {
    private static final String MANIFEST = "META-INF/MANIFEST.MF";

    public static JarFile getJarFile(final ClassLoader loader) {
        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;
    }
}

Related

  1. findJarsNamesInPath(String path)
  2. getJarFile(@Nonnull Class clazz)
  3. getJarFile(Class clazz)
  4. getJarFile(Class clazz)
  5. getJarFile(Class clazz)
  6. getJarFile(String classPath)
  7. getJarFileByResourceName(Class clazz, String resourceName)
  8. getJarFolder(Class clazz)
  9. getJarLocation()