title: Java : programmatically determine all of the package names loaded on the classpath
body: <p>Any suggestions as to how to approach how I would find out a list of <strong>package names</strong> that exist on the <strong>current classpath</strong>?</p>
  
  <p>This needs to be done programmatically at run time by one of the classes loaded (and executing) on the classpath (i.e. <strong>inside out</strong>, not outside in).</p>
  
  <hr>
  
  <p><strong>More details:</strong></p>
  
  <p>One approach I considered was to use reflection on each of the classes loaded thus far by the class loader, and extract the package names from them. However, my application already runs into thousands of classes, so I need a more efficient approach.</p>
  
  <p>Another thing I considered was something analogous to finding out what JAR files were in the class path, and then do the directory listing parallel for each JAR. However, I don't know if this is possible from within the application/ how to do it.</p>
  
  <p><strong>Bonus points</strong></p>
  
  <p>Bonus points for anyone who suggests a way that can filter by top-level packages. E.g. show all packages that come under <code>com.xyz</code> ==> <code>com.xyz.*</code>, <code>com.xyz.*.*</code></p>
  
  <p>Thanks!</p>