Example usage for java.util.jar JarFile JarFile

List of usage examples for java.util.jar JarFile JarFile

Introduction

In this page you can find the example usage for java.util.jar JarFile JarFile.

Prototype

public JarFile(File file) throws IOException 

Source Link

Document

Creates a new JarFile to read from the specified File object.

Usage

From source file:com.cwctravel.jenkinsci.plugins.htmlresource.HTMLResourceManagement.java

private boolean validateWebJAR(File file) throws IOException {
    boolean result = false;
    if (file.getName().endsWith(".jar")) {
        JarFile jarFile = new JarFile(file);
        try {//from  w w w .  j  a v  a 2  s.  c  om
            result = jarFile.size() > 0;
        } finally {
            jarFile.close();
        }
    }

    return result;
}

From source file:com.eucalyptus.upgrade.TestHarness.java

@SuppressWarnings("unchecked")
private static Multimap<Class, Method> getTestMethods() throws Exception {
    final Multimap<Class, Method> testMethods = ArrayListMultimap.create();
    List<Class> classList = Lists.newArrayList();
    for (File f : new File(System.getProperty("euca.home") + "/usr/share/eucalyptus").listFiles()) {
        if (f.getName().startsWith("eucalyptus") && f.getName().endsWith(".jar")
                && !f.getName().matches(".*-ext-.*")) {
            try {
                JarFile jar = new JarFile(f);
                Enumeration<JarEntry> jarList = jar.entries();
                for (JarEntry j : Collections.list(jar.entries())) {
                    if (j.getName().matches(".*\\.class.{0,1}")) {
                        String classGuess = j.getName().replaceAll("/", ".").replaceAll("\\.class.{0,1}", "");
                        try {
                            Class candidate = ClassLoader.getSystemClassLoader().loadClass(classGuess);
                            for (final Method m : candidate.getDeclaredMethods()) {
                                if (Iterables.any(testAnnotations,
                                        new Predicate<Class<? extends Annotation>>() {
                                            public boolean apply(Class<? extends Annotation> arg0) {
                                                return m.getAnnotation(arg0) != null;
                                            }
                                        })) {
                                    System.out.println("Added test class: " + candidate.getCanonicalName());
                                    testMethods.put(candidate, m);
                                }/*from   w w  w . java2 s . c om*/
                            }
                        } catch (ClassNotFoundException e) {
                        }
                    }
                }
                jar.close();
            } catch (Exception e) {
                System.out.println(e.getMessage());
                continue;
            }
        }
    }
    return testMethods;
}

From source file:net.fabricmc.loader.FabricLoader.java

protected static ModInfo[] getJarMods(File f) {
    try {/*www.  j  a  va2 s.  c om*/
        JarFile jar = new JarFile(f);
        ZipEntry entry = jar.getEntry("mod.json");
        if (entry != null) {
            try (InputStream in = jar.getInputStream(entry)) {
                return getMods(in);
            }
        }

    } catch (Exception e) {
        LOGGER.error("Unable to load mod from %s", f.getName());
        e.printStackTrace();
    }

    return new ModInfo[0];
}

From source file:org.araqne.pkg.PackageManagerService.java

private String getBundleSymbolicName(File file) {
    JarFile jar = null;/*w  w  w  .  ja  va  2s . c  o  m*/
    try {
        jar = new JarFile(file);
        Attributes attrs = jar.getManifest().getMainAttributes();
        // metadata can be added followed by semicolon (e.g. ;singleton)
        return attrs.getValue("Bundle-SymbolicName").split(";")[0].trim();
    } catch (IOException e) {
        logger.error("package manager: symbolic name not found", e);
        return null;
    } finally {
        if (jar != null)
            try {
                jar.close();
            } catch (IOException e) {
            }
    }
}

From source file:org.eclipse.emf.mwe.utils.StandaloneSetup.java

protected void registerBundle(File file) {
    JarFile jarFile = null;/*from w  w  w  . j a  va 2  s  . co  m*/
    try {
        jarFile = new JarFile(file);
        log.debug("Trying to determine project name from Manifest for " + jarFile.getName());
        String name = getBundleNameFromManifest(jarFile);
        if (name == null) {
            log.debug("Trying to determine project name from file name for " + jarFile.getName());
            name = getBundleNameFromJarName(jarFile.getName());
        }
        if (name != null) {
            final int indexOf = name.indexOf(';');
            if (indexOf > 0)
                name = name.substring(0, indexOf);
            String path = "archive:" + file.getCanonicalFile().toURI() + "!/";
            URI uri = URI.createURI(path);
            registerMapping(name, uri);
        } else {
            log.debug("Could not determine project name for " + jarFile.getName()
                    + ". No project mapping will be added.");
        }
    } catch (ZipException e) {
        log.warn("Could not open Jar file " + file.getAbsolutePath() + ".");
    } catch (Exception e) {
        handleException(file, e);
    } finally {
        try {
            if (jarFile != null)
                jarFile.close();
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
    }
}

From source file:org.ow2.proactive_grid_cloud_portal.scheduler.server.SchedulerServiceImpl.java

private boolean isJarFile(File file) {
    try {//  w  w w  .java2s. co  m
        new JarFile(file);
        return true;
    } catch (IOException e1) {
        return false;
    }
}

From source file:org.apache.struts2.osgi.host.BaseOsgiHost.java

/**
 * Gets the version used to export the packages. it tries to get it from MANIFEST.MF, or the file name
 *//*w w w  .  j  ava2  s .  c  o m*/
protected String getVersion(URL url) {
    if ("jar".equals(url.getProtocol())) {
        try {
            FileManager fileManager = ServletActionContext.getContext().getInstance(FileManagerFactory.class)
                    .getFileManager();
            JarFile jarFile = new JarFile(new File(fileManager.normalizeToFileProtocol(url).toURI()));
            Manifest manifest = jarFile.getManifest();
            if (manifest != null) {
                String version = manifest.getMainAttributes().getValue("Bundle-Version");
                if (StringUtils.isNotBlank(version)) {
                    return getVersionFromString(version);
                }
            } else {
                //try to get the version from the file name
                return getVersionFromString(jarFile.getName());
            }
        } catch (Exception e) {
            if (LOG.isErrorEnabled()) {
                LOG.error("Unable to extract version from [#0], defaulting to '1.0.0'", url.toExternalForm());
            }
        }
    }
    return "1.0.0";
}

From source file:org.araqne.pkg.PackageManagerService.java

private Version getBundleVersion(File file) {
    JarFile jar = null;/*from  w  ww .ja  va  2  s .  c  o  m*/
    try {
        jar = new JarFile(file);
        Attributes attrs = jar.getManifest().getMainAttributes();
        return new Version(attrs.getValue("Bundle-Version"));
    } catch (IOException e) {
        logger.error("package manager: bundle version not found", e);
        return null;
    } finally {
        if (jar != null)
            try {
                jar.close();
            } catch (IOException e) {
            }
    }
}

From source file:org.apache.openejb.config.Deploy.java

private static boolean shouldUnpack(final File file) {
    final String name = file.getName();
    if (name.endsWith(".ear") || name.endsWith(".rar") || name.endsWith(".rar")) {
        return true;
    }/*from  ww  w .jav a2  s  .  co m*/

    JarFile jarFile = null;
    try {
        jarFile = new JarFile(file);

        if (jarFile.getEntry("META-INF/application.xml") != null) {
            return true;
        }
        if (jarFile.getEntry("META-INF/ra.xml") != null) {
            return true;
        }
        if (jarFile.getEntry("WEB-INF/web.xml") != null) {
            return true;
        }
    } catch (final IOException e) {
        // no-op
    } finally {
        if (jarFile != null) {
            try {
                jarFile.close();
            } catch (final IOException ignored) {
                // no-op
            }
        }
    }

    return false;
}

From source file:edu.stanford.muse.email.JarDocCache.java

public Set<Integer> getAllContentIdxs(String prefix) throws IOException, ClassNotFoundException {
    Set<Integer> result = new LinkedHashSet<Integer>();
    JarFile jarFile = null;/*  w  w w  . j  a  v a 2  s .com*/
    String fname = baseDir + File.separator + prefix + ".contents";
    try {
        jarFile = new JarFile(fname);
    } catch (Exception e) {
        log.info("No Jar file exists: " + fname);
    }
    if (jarFile == null)
        return result;

    Enumeration<JarEntry> entries = jarFile.entries();
    String suffix = ".content";
    while (entries.hasMoreElements()) {
        JarEntry je = entries.nextElement();
        String s = je.getName();
        if (!s.endsWith(suffix))
            continue;
        String idx_str = s.substring(0, s.length() - suffix.length());
        int index = -1;
        try {
            index = Integer.parseInt(idx_str);
        } catch (Exception e) {
            log.error("Funny file in header: " + index);
        }

        result.add(index);
    }
    return result;
}