Java Utililty Methods Class from Package

List of utility methods to do Class from Package

Description

The list of methods to do Class from Package are organized into topic(s).

Method

List>getClasses(String packageName)
Locates all classes in given package
String path = packageName.replace('.', '/');
List<URL> dirs = getDirectories(path);
ArrayList<Class<?>> classes = new ArrayList<Class<?>>();
ClassLoader loader = URLClassLoader.newInstance(dirs.toArray(new URL[0]),
        Thread.currentThread().getContextClassLoader());
for (URL directory : dirs) {
    String resource = directory.getPath().replace("/" + path + "/", "");
    if (resource.endsWith(".jar")) {
...
Class[]getClasses(String packageName)
Returns an array of classes in a specified package
try {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    assert classLoader != null;
    String path = packageName.replace('.', '/');
    Enumeration<URL> resources = classLoader.getResources(path);
    List<File> dirs = new ArrayList<File>();
    while (resources.hasMoreElements()) {
        URL resource = resources.nextElement();
...
IterablegetClasses(String packageName)
get Classes
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
String path = packageName.replace('.', '/');
Enumeration<URL> resources = classLoader.getResources(path);
List<File> dirs = new ArrayList<File>();
while (resources.hasMoreElements()) {
    URL resource = resources.nextElement();
    dirs.add(new File(resource.getFile()));
List<Class> classes = new ArrayList<Class>();
for (File directory : dirs) {
    classes.addAll(findClasses(directory, packageName));
return classes;
Class[]getClasses(String packageName)
get Classes
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
assert classLoader != null;
String path = packageName.replace('.', '/');
Enumeration<URL> resources = classLoader.getResources(path);
List<File> dirs = new ArrayList<File>();
while (resources.hasMoreElements()) {
    URL resource = resources.nextElement();
    dirs.add(new File(java.net.URLDecoder.decode(resource.getFile().toString())));
...
ListgetClasses(String packageName)
get Classes
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
String path = packageName.replace(".", "/");
Enumeration<URL> resources = null;
try {
    resources = classLoader.getResources(path);
} catch (IOException e) {
    e.printStackTrace();
List<File> dirs = new ArrayList<>();
if (resources != null) {
    while (resources.hasMoreElements()) {
        URL resource = resources.nextElement();
        dirs.add(new File(resource.getFile()));
ArrayList<Class> classes = new ArrayList<>();
for (File directory : dirs) {
    classes.addAll(findClass(directory, packageName));
return classes;
Class[]getClasses(String packageName)
Returns all classes from specified package.
ArrayList<Class> classes = new ArrayList<Class>();
File directory = null;
try {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    if (classLoader == null) {
        throw new ClassNotFoundException("Can't get class loader.");
    String path = packageName.replace('.', '/');
...
ListgetClasses(String packageName)
Scans all classes accessible from the context class loader which belong to the given package and subpackages.
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
assert classLoader != null;
String path = packageName.replace('.', '/');
Enumeration<URL> resources = classLoader.getResources(path);
List<File> dirs = new ArrayList<File>();
while (resources.hasMoreElements()) {
    URL resource = resources.nextElement();
    String fileName = resource.getFile();
...
Class[]getClasses(String packageName)
Scans all classes accessible from the context class loader which belong to the given package and subpackages.
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
assert classLoader != null;
String path = packageName.replace('.', '/');
Enumeration<URL> resources = classLoader.getResources(path);
List<File> dirs = new ArrayList<File>();
while (resources.hasMoreElements()) {
    URL resource = resources.nextElement();
    dirs.add(new File(resource.getFile()));
...
ArrayList>getClasses(String packageName, Class inheritedType)
Scans all classes accessible from the context class loader which belong to the given package and subpackages.
ArrayList<Class<? extends T>> result = new ArrayList<Class<? extends T>>();
ArrayList<Class<?>> classes = new ArrayList<Class<?>>();
try {
    classes = getClasses(packageName);
} catch (ClassNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
...
ListgetClasses(String pckgname)
get Classes
List<Class> classes = new ArrayList<Class>();
ClassLoader cld = Thread.currentThread().getContextClassLoader();
String path = '/' + pckgname.replace('.', '/');
URL resource = cld.getResource(path);
File directory = new File(resource.getFile());
if (directory.exists()) {
    String[] files = directory.list();
    for (int i = 0; i < files.length; i++) {
...