Here you can find the source of getClassNameByJar(String jarPath, boolean childPackage)
private static List<String> getClassNameByJar(String jarPath, boolean childPackage)
//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; } }