Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Unfortunately, Java doesn't provide an easy way to list classes in the "native" JRE. That leaves you with a couple of options: (a) for any given JAR file, you can list the entries inside that JAR file, find the <code>.class</code> files, and then determine which Java class each <code>.class</code> file represents; or (b) you can use a library that does this for you.</p> <h2>Option (a): Scanning JAR files manually</h2> <p>In this option, we'll fill <code>classNames</code> with the list of all Java classes contained inside a jar file at <code>/path/to/jar/file.jar</code>.</p> <pre><code>List&lt;String&gt; classNames = new ArrayList&lt;String&gt;(); ZipInputStream zip = new ZipInputStream(new FileInputStream("/path/to/jar/file.jar")); for (ZipEntry entry = zip.getNextEntry(); entry != null; entry = zip.getNextEntry()) { if (!entry.isDirectory() &amp;&amp; entry.getName().endsWith(".class")) { // This ZipEntry represents a class. Now, what class does it represent? String className = entry.getName().replace('/', '.'); // including ".class" classNames.add(className.substring(0, className.length() - ".class".length())); } } </code></pre> <h2>Option (b): Using specialized reflections libraries</h2> <h3>Guava</h3> <p><a href="https://github.com/google/guava">Guava</a> has had <a href="https://google.github.io/guava/releases/14.0/api/docs/com/google/common/reflect/ClassPath.html"><code>ClassPath</code></a> since at least 14.0, which I have used and liked. One nice thing about <code>ClassPath</code> is that it doesn't load the classes it finds, which is important when you're scanning for a large number of classes.</p> <pre><code>ClassPath cp=ClassPath.from(Thread.currentThread().getContextClassLoader()); for(ClassPath.ClassInfo info : cp.getTopLevelClassesRecurusive("my.package.name")) { // Do stuff with classes here... } </code></pre> <h3>Reflections</h3> <p>I haven't personally used <a href="https://github.com/ronmamo/reflections">the Reflections library</a>, but it seems well-liked. Some great examples are provided on the website like this quick way to load all the classes in a package provided by <em>any</em> JAR file, which may also be useful for your application.</p> <pre><code>Reflections reflections = new Reflections("my.project.prefix"); Set&lt;Class&lt;? extends SomeType&gt;&gt; subTypes = reflections.getSubTypesOf(SomeType.class); Set&lt;Class&lt;?&gt;&gt; annotated = reflections.getTypesAnnotatedWith(SomeAnnotation.class); </code></pre>
    singulars
    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. 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.
    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