Code example for JarFile

Methods: getInputStreamgetJarEntry, close

0
public class JarUtils { 
    @NonNls 
    public static final String MANIFEST_PATH = "META-INF/MANIFEST.MF";
 
    private static String getSpecificationVersion(JarFile jarFile) throws IOException {
        JarEntry jarEntry = jarFile.getJarEntry(MANIFEST_PATH);
        if (jarEntry == null) {
            return null; 
        } 
        final InputStream inputStream = jarFile.getInputStream(jarEntry);
        Manifest manifest;
        try { 
            manifest = new Manifest(inputStream);
        } finally { 
            inputStream.close();
        } 
 
        final String version = manifest.getAttributes("Play").getValue((Attributes.Name.SPECIFICATION_VERSION));
        if (version != null) {
            return version;
        } 
 
        return null; 
    } 
 
    @Nullable 
    public static String getSpecificationVersion(File file) {
        try { 
            JarFile jarFile = new JarFile(file);
            try { 
                return getSpecificationVersion(jarFile);
            } finally { 
                jarFile.close();
            } 
        } catch (Exception e) {
            return null; 
        } 
    } 
Contextual code suggestions in your IDE  Get Codota for Java