Here you can find the source of scanPackageByJar(List
private static void scanPackageByJar(List<Class<?>> classes, final String packageName, final URL jarUrl, final boolean recursive)
//package com.java2s; //License from project: Open Source License import java.io.IOException; import java.net.JarURLConnection; import java.net.URL; import java.util.Enumeration; import java.util.List; import java.util.jar.JarEntry; import java.util.jar.JarFile; public class Main { private static void scanPackageByJar(List<Class<?>> classes, final String packageName, final URL jarUrl, final boolean recursive) { String packagePath = packageName.replace(".", "/"); try {/*from w w w . j av a2s.c o m*/ JarFile jarFile = ((JarURLConnection) jarUrl.openConnection()).getJarFile(); Enumeration<JarEntry> entrys = jarFile.entries(); while (entrys.hasMoreElements()) { JarEntry jarEntry = entrys.nextElement(); String entryName = jarEntry.getName(); if (!entryName.startsWith(packagePath) || jarEntry.isDirectory()) { continue; } if (!recursive && entryName.lastIndexOf('/') != packagePath.length()) { continue; } String clazzSimpleName = entryName.substring(entryName.lastIndexOf('/') + 1); if (filterClassName(clazzSimpleName)) { String clazzName = entryName.replace('/', '.'); clazzName = clazzName.substring(0, clazzName.length() - ".class".length()); Class<?> clazz = null; try { clazz = Thread.currentThread().getContextClassLoader().loadClass(clazzName); } catch (ClassNotFoundException e) { e.printStackTrace(); } if (clazz != null) { classes.add(clazz); } } } } catch (IOException e) { e.printStackTrace(); } } private static boolean filterClassName(final String fileName) { return fileName.endsWith(".class"); } }