Here you can find the source of getAllClasses(ClassLoader cl, String packageName)
public static List<String> getAllClasses(ClassLoader cl, String packageName)
//package com.java2s; /******************************************************************************* * Copyright (C) 2017, 2018 wysohn//from w w w.j a v a 2 s.co m * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. *******************************************************************************/ import java.io.File; import java.io.IOException; import java.net.URL; import java.net.URLClassLoader; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipException; import java.util.zip.ZipFile; public class Main { public static List<String> getAllClasses(ClassLoader cl, String packageName) { packageName = packageName.replace('.', '/'); List<String> classes = new ArrayList<>(); URL[] urls = ((URLClassLoader) cl).getURLs(); for (URL url : urls) { //System.out.println(url.getFile()); File jar = new File(url.getFile()); if (jar.isDirectory()) { File subdir = new File(jar, packageName); if (!subdir.exists()) continue; File[] files = subdir.listFiles(); for (File file : files) { if (!file.isFile()) continue; if (file.getName().endsWith(".class")) classes.add(file.getName().substring(0, file.getName().length() - 6).replace('/', '.')); } } else { // try to open as ZIP try { ZipFile zip = new ZipFile(jar); for (Enumeration<? extends ZipEntry> entries = zip.entries(); entries.hasMoreElements();) { ZipEntry entry = entries.nextElement(); String name = entry.getName(); if (!name.startsWith(packageName)) continue; if (name.endsWith(".class") && name.indexOf('$') < 0) classes.add(name.substring(0, name.length() - 6).replace('/', '.')); } zip.close(); } catch (ZipException e) { //System.out.println("Not a ZIP: " + e.getMessage()); } catch (IOException e) { System.err.println(e.getMessage()); } } } return classes; } }