Here you can find the source of getClassesForPackage(String pckgname)
public static List<Class<?>> getClassesForPackage(String pckgname) throws ClassNotFoundException
//package com.java2s; /*// w ww .j a v a2 s . co m * Copyright (c) 2004-2013 Universidade do Porto - Faculdade de Engenharia * Laborat?rio de Sistemas e Tecnologia Subaqu?tica (LSTS) * All rights reserved. * Rua Dr. Roberto Frias s/n, sala I203, 4200-465 Porto, Portugal * * This file is part of Neptus, Command and Control Framework. * * Commercial Licence Usage * Licencees holding valid commercial Neptus licences may use this file * in accordance with the commercial licence agreement provided with the * Software or, alternatively, in accordance with the terms contained in a * written agreement between you and Universidade do Porto. For licensing * terms, conditions, and further information contact lsts@fe.up.pt. * * European Union Public Licence - EUPL v.1.1 Usage * Alternatively, this file may be used under the terms of the EUPL, * Version 1.1 only (the "Licence"), appearing in the file LICENCE.md * included in the packaging of this file. You may not use this work * except in compliance with the Licence. Unless required by applicable * law or agreed to in writing, software distributed under the Licence is * distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF * ANY KIND, either express or implied. See the Licence for the specific * language governing permissions and limitations at * https://www.lsts.pt/neptus/licence. * * For more information please see <http://lsts.fe.up.pt/neptus>. * * Author: * 20??/??/?? */ import java.io.File; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URL; import java.net.URLDecoder; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; import java.util.jar.JarFile; public class Main { public static List<Class<?>> getClassesForPackage(String pckgname) throws ClassNotFoundException { return getClassesForPackage(pckgname, true); } /** * Attempts to list all the classes in the specified package as determined by the context class loader * * @param pckgname the package name to search * @return a list of classes that exist within that package * @throws ClassNotFoundException if something went wrong */ public static List<Class<?>> getClassesForPackage(String pckgname, boolean recursive) throws ClassNotFoundException { // This will hold a list of directories matching the pckgname. There may be more than one if a package is split // over multiple jars/paths ArrayList<File> directories = new ArrayList<File>(); ArrayList<Class<?>> classes = new ArrayList<Class<?>>(); // NeptusLog.pub().info("<###>evaluating "+pckgname); try { ClassLoader cld = Thread.currentThread().getContextClassLoader(); if (cld == null) { throw new ClassNotFoundException("Can't get class loader."); } String path = pckgname.replace('.', '/'); // System.err.println(path); // Ask for all resources for the path Enumeration<URL> resources = cld.getResources(path); while (resources.hasMoreElements()) { String res = resources.nextElement().getPath(); if (res.contains(".jar!")) { // res = res.replaceAll("%20", " "); // String path = res.substring(0, res.indexOf("r!")).replaceAll(".", replacement) JarFile zf = new JarFile(URLDecoder.decode(res.substring(5, res.indexOf("!")), "UTF-8")); Enumeration<?> enumer = zf.entries(); while (enumer.hasMoreElements()) { String nextPath = enumer.nextElement().toString(); String next = nextPath.replaceAll("/", "."); if (next.startsWith(pckgname) && next.endsWith(".class")) { String clazz = next.substring(0, next.length() - 6); classes.add(Class.forName(clazz)); } else if (recursive) { classes.addAll(getClassesForPackage(res, true)); } } zf.close(); } else directories.add(new File(URLDecoder.decode(res, "UTF-8"))); } } catch (NullPointerException x) { throw new ClassNotFoundException( pckgname + " does not appear to be a valid package (Null pointer exception)"); } catch (UnsupportedEncodingException encex) { throw new ClassNotFoundException( pckgname + " does not appear to be a valid package (Unsupported encoding)"); } catch (IOException ioex) { ioex.printStackTrace(); throw new ClassNotFoundException( "IOException was thrown when trying to get all resources for " + pckgname); } catch (Exception e) { e.printStackTrace(System.err); } for (File directory : directories) { // NeptusLog.pub().info("<###> "+directory); if (directory.exists()) { // Get the list of the files contained in the package String[] files = directory.list(); for (String file : files) { // we are only interested in .class files if (file.endsWith(".class")) { // removes the .class extension classes.add(Class.forName(pckgname + '.' + file.substring(0, file.length() - 6))); } else if (recursive) { classes.addAll(getClassesForPackage(pckgname + '.' + file, true)); } } } else { throw new ClassNotFoundException( pckgname + " (" + directory.getPath() + ") does not appear to be a valid package"); } } return classes; } }