Java Jar Usage getClassNameByJar(String jarPath, boolean childPackage)

Here you can find the source of getClassNameByJar(String jarPath, boolean childPackage)

Description

get Class Name By Jar

License

Open Source License

Declaration

private static List<String> getClassNameByJar(String jarPath, boolean childPackage) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.*;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class Main {

    private static List<String> getClassNameByJar(String jarPath, boolean childPackage) {
        List<String> myClassName = new ArrayList<String>();
        String[] jarInfo = jarPath.split("!");
        String jarFilePath = jarInfo[0].substring(jarInfo[0].indexOf("/"));
        String packagePath = jarInfo[1].substring(1);
        try {//from ww w  .  ja va 2s.  co  m
            JarFile jarFile = new JarFile(jarFilePath);
            Enumeration<JarEntry> entrys = jarFile.entries();
            while (entrys.hasMoreElements()) {
                JarEntry jarEntry = entrys.nextElement();
                String entryName = jarEntry.getName();
                if (entryName.endsWith(".class")) {
                    if (childPackage) {
                        if (entryName.startsWith(packagePath)) {
                            entryName = entryName.replace("/", ".").substring(0, entryName.lastIndexOf("."));
                            myClassName.add(entryName);
                        }
                    } else {
                        int index = entryName.lastIndexOf("/");
                        String myPackagePath;
                        if (index != -1) {
                            myPackagePath = entryName.substring(0, index);
                        } else {
                            myPackagePath = entryName;
                        }
                        if (myPackagePath.equals(packagePath)) {
                            entryName = entryName.replace("/", ".").substring(0, entryName.lastIndexOf("."));
                            myClassName.add(entryName);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return myClassName;
    }
}

Related

  1. containsClass(JarFile jarFile, String classFilePath)
  2. convertAttributes(Attributes attributes)
  3. createPacker()
  4. findClassesByJar(String packageName, JarFile jar, final boolean recursive, Set> classes)
  5. getAttribute(String name, String value)
  6. getEntriesName(String jarFileName)
  7. getFileNamesWithSuffixForJarFile(JarFile jar, String suffix)
  8. getInfo(String jarFileName)
  9. getJarInFileList(JarFile jarFile, String targetPath)