Java Jar Manifest getManifestVersionNumber(File file)

Here you can find the source of getManifestVersionNumber(File file)

Description

get Manifest Version Number

License

Apache License

Declaration

static public String getManifestVersionNumber(File file) throws IOException 

Method Source Code


//package com.java2s;
/*/* ww  w .j  ava 2s .co m*/
 * #%L
 * jne
 * %%
 * Copyright (C) 2015 Fizzed, Inc
 * %%
 * 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.
 * #L%
 */

import java.io.File;
import java.io.IOException;

import java.util.Iterator;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;

public class Main {
    static public String getManifestVersionNumber(File file) throws IOException {
        JarFile jar = new JarFile(file);
        Manifest manifest = jar.getManifest();
        String versionNumber = null;
        java.util.jar.Attributes attributes = manifest.getMainAttributes();
        if (attributes != null) {
            Iterator it = attributes.keySet().iterator();
            while (it.hasNext()) {
                Attributes.Name key = (Attributes.Name) it.next();
                String keyword = key.toString();
                if (keyword.equals("Implementation-Version") || keyword.equals("Bundle-Version")) {
                    versionNumber = (String) attributes.get(key);
                    break;
                }
            }
        }
        jar.close();

        if (versionNumber == null || versionNumber.equals("")) {
            return null;
        }

        return versionNumber;
    }
}

Related

  1. getManifestProperty(ClassLoader clContainingManifest, String manifestKey)
  2. getManifestProperty(File file, String name)
  3. getManifestProperty(Manifest manifest, String propertyName)
  4. getManifestValue(File jarFile, String key)
  5. getManifestValue(JarFile jarFile, String key, String defaultValue)
  6. getValueFromAttr(Attributes attributes, String manifestValue)
  7. isOSGiManifest(Manifest manifest)
  8. manifestToProperties(Attributes d)
  9. merge(Manifest into, Manifest from)