Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I've written some code to do most of the work. It works only if your files are on the local disk instead of in a JAR. All you need is one class in the package. You could, for this purpose, create a Locator.java class, just to be able to find the package.</p> <pre><code>public class ClassEnumerator { public static void main(String[] args) throws ClassNotFoundException { List&lt;Class&lt;?&gt;&gt; list = listClassesInSamePackage(Locator.class, true); System.out.println(list); } private static List&lt;Class&lt;?&gt;&gt; listClassesInSamePackage(Class&lt;?&gt; locator, boolean includeLocator) throws ClassNotFoundException { File packageFile = getPackageFile(locator); String ignore = includeLocator ? null : locator.getSimpleName() + ".class"; return toClassList(locator.getPackage().getName(), listClassNames(packageFile, ignore)); } private static File getPackageFile(Class&lt;?&gt; locator) { URL url = locator.getClassLoader().getResource(locator.getName().replace(".", "/") + ".class"); if (url == null) { throw new RuntimeException("Cannot locate " + Locator.class.getName()); } try { return new File(url.toURI()).getParentFile(); } catch (URISyntaxException e) { throw new RuntimeException(e); } } private static String[] listClassNames(File packageFile, final String ignore) { return packageFile.list(new FilenameFilter(){ @Override public boolean accept(File dir, String name) { if (name.equals(ignore)) { return false; } return name.endsWith(".class"); } }); } private static List&lt;Class&lt;?&gt;&gt; toClassList(String packageName, String[] classNames) throws ClassNotFoundException { List&lt;Class&lt;?&gt;&gt; result = new ArrayList&lt;Class&lt;?&gt;&gt;(classNames.length); for (String className : classNames) { // Strip the .class String simpleName = className.substring(0, className.length() - 6); result.add(Class.forName(packageName + "." + simpleName)); } return result; } } </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