Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could use <a href="https://web.archive.org/web/20090227113602/http://snippets.dzone.com:80/posts/show/4831" rel="noreferrer">this method</a><sup>1</sup> that uses the <code>ClassLoader</code>.</p> <pre><code> /** * Scans all classes accessible from the context class loader which belong to the given package and subpackages. * * @param packageName The base package * @return The classes * @throws ClassNotFoundException * @throws IOException */ private static Class[] getClasses(String packageName) throws ClassNotFoundException, IOException { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); assert classLoader != null; String path = packageName.replace('.', '/'); Enumeration&lt;URL&gt; resources = classLoader.getResources(path); List&lt;File&gt; dirs = new ArrayList&lt;File&gt;(); while (resources.hasMoreElements()) { URL resource = resources.nextElement(); dirs.add(new File(resource.getFile())); } ArrayList&lt;Class&gt; classes = new ArrayList&lt;Class&gt;(); for (File directory : dirs) { classes.addAll(findClasses(directory, packageName)); } return classes.toArray(new Class[classes.size()]); } /** * Recursive method used to find all classes in a given directory and subdirs. * * @param directory The base directory * @param packageName The package name for classes found inside the base directory * @return The classes * @throws ClassNotFoundException */ private static List&lt;Class&gt; findClasses(File directory, String packageName) throws ClassNotFoundException { List&lt;Class&gt; classes = new ArrayList&lt;Class&gt;(); if (!directory.exists()) { return classes; } File[] files = directory.listFiles(); for (File file : files) { if (file.isDirectory()) { assert !file.getName().contains("."); classes.addAll(findClasses(file, packageName + "." + file.getName())); } else if (file.getName().endsWith(".class")) { classes.add(Class.forName(packageName + '.' + file.getName().substring(0, file.getName().length() - 6))); } } return classes; } </code></pre> <p>__________<br> <sub><sup>1</sup> This method was taken originally from <a href="http://snippets.dzone.com/posts/show/4831" rel="noreferrer">http://snippets.dzone.com/posts/show/4831</a>, which was <a href="https://web.archive.org/web/20090227113602/http://snippets.dzone.com:80/posts/show/4831" rel="noreferrer">archived</a> by the Internet Archive, as linked to now. The snippet is also available at <a href="https://dzone.com/articles/get-all-classes-within-package" rel="noreferrer">https://dzone.com/articles/get-all-classes-within-package</a>.</sub></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload