Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's a more complete way to solve this for jars, based on the idea posted by JG.</p> <pre><code>/** * Scans all classloaders for the current thread for loaded jars, and then scans * each jar for the package name in question, listing all classes directly under * the package name in question. Assumes directory structure in jar file and class * package naming follow java conventions (i.e. com.example.test.MyTest would be in * /com/example/test/MyTest.class) */ public Collection&lt;Class&gt; getClassesForPackage(String packageName) throws Exception { String packagePath = packageName.replace(".", "/"); ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); Set&lt;URL&gt; jarUrls = new HashSet&lt;URL&gt;(); while (classLoader != null) { if (classLoader instanceof URLClassLoader) for (URL url : ((URLClassLoader) classLoader).getURLs()) if (url.getFile().endsWith(".jar") // may want better way to detect jar files jarUrls.add(url); classLoader = classLoader.getParent(); } Set&lt;Class&gt; classes = new HashSet&lt;Class&gt;(); for (URL url : jarUrls) { JarInputStream stream = new JarInputStream(url.openStream()); // may want better way to open url connections JarEntry entry = stream.getNextJarEntry(); while (entry != null) { String name = entry.getName(); int i = name.lastIndexOf("/"); if (i &gt; 0 &amp;&amp; name.endsWith(".class") &amp;&amp; name.substring(0, i).equals(packagePath)) classes.add(Class.forName(name.substring(0, name.length() - 6).replace("/", "."))); entry = stream.getNextJarEntry(); } stream.close(); } return classes; } </code></pre>
 

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