Java Class Name from Jar File gatherClassnamesFromJar(File jar)

Here you can find the source of gatherClassnamesFromJar(File jar)

Description

gather Classnames From Jar

License

Apache License

Declaration

public static Set<String> gatherClassnamesFromJar(File jar) throws IOException 

Method Source Code

//package com.java2s;
/********************************************************************************
 * Copyright (c) 2011-2017 Red Hat Inc. and/or its affiliates and others
 *
 * This program and the accompanying materials are made available under the 
 * terms of the Apache License, Version 2.0 which is available at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * SPDX-License-Identifier: Apache-2.0 /*from ww  w. ja v a  2 s .  c o m*/
 ********************************************************************************/

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

import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class Main {
    public static Set<String> gatherClassnamesFromJar(File jar) throws IOException {
        HashSet<String> names = new HashSet<>();
        try (JarFile zf = new JarFile(jar)) {
            Enumeration<? extends JarEntry> entries = zf.entries();
            while (entries.hasMoreElements()) {
                JarEntry entry = entries.nextElement();
                if (!entry.isDirectory() && entry.getName().endsWith(".class")
                // skip those because ClassLoader.findClass barfs on them
                        && !entry.getName().endsWith("module-info.class")) {
                    String name = entry.getName();
                    String className = name.substring(0, name.length() - 6).replace('/', '.');
                    names.add(className);
                }
            }
        }
        return names;
    }
}