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

Class[]getClasses(String pckgname)
list Classes inside a given package.
ArrayList classes = new ArrayList();
File directory = null;
try {
    URL str = Thread.currentThread().getContextClassLoader().getResource(pckgname.replace('.', '/'));
    System.out.println("instance advice..." + new File(str.getFile()).getName());
    directory = new File(str.getFile());
} catch (NullPointerException x) {
    throw new ClassNotFoundException(pckgname + " does not appear to be a valid package");
...
SetgetClasses(String pkg)
Returns a set of all class files for given package.
Set<File> classes = new HashSet<File>();
ClassLoader cl = Thread.currentThread().getContextClassLoader();
List<URL> urls = Collections.list(cl.getResources(pkg.replaceAll("\\.", "/")));
for (URL url : urls) {
    File dir = new File(url.toURI());
    if (dir.isDirectory()) {
        for (File file : dir.listFiles()) {
            if (file.isFile() && file.getAbsolutePath().endsWith(".class")) {
...
List>getClasses(String pkg)
get Classes
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
String path = pkg.replace('.', '/');
Enumeration<URL> resources = classLoader.getResources(path);
List<Class<?>> classes = new ArrayList<Class<?>>();
while (resources.hasMoreElements()) {
    URL resource = resources.nextElement();
    if (resource.getProtocol().equalsIgnoreCase(PROTOCOL_JAR)) {
        try {
...
List>getClassesForPackage(final String packageName)
Attempts to list all the classes in the specified package as determined by the context class loader.
ArrayList<File> directories = new ArrayList<File>();
try {
    ClassLoader cld = Thread.currentThread().getContextClassLoader();
    if (cld == null) {
        throw new ClassNotFoundException("Can't get class loader.");
    String path = packageName.replace('.', '/');
    Enumeration<URL> resources = cld.getResources(path);
...
List>getClassesForPackage(Package pkg)
get Classes For Package
String pkgname = pkg.getName();
ArrayList<Class<?>> classes = new ArrayList<Class<?>>();
File directory = null;
String fullPath;
String relPath = pkgname.replace('.', '/');
System.out.println("ClassDiscovery: Package: " + pkgname + " becomes Path:" + relPath);
URL resource = ClassLoader.getSystemClassLoader().getResource(relPath);
System.out.println("ClassDiscovery: Resource = " + resource);
...
List>getClassesForPackage(String packageName)
get Classes For Package
List<Class<?>> classes = new ArrayList<Class<?>>();
List<File> directories = new ArrayList<File>();
try {
    ClassLoader cld = Thread.currentThread().getContextClassLoader();
    if (cld == null) {
        throw new ClassNotFoundException("Can't get class loader.");
    Enumeration<URL> resources = cld.getResources(packageName.replace('.', '/'));
...
Set>getClassesForPackage(String packageName)
get Classes For Package
Set<Class<?>> classes = new HashSet<Class<?>>();
JarInputStream jarInputStream = null;
try {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    Enumeration<URL> resources = classLoader.getResources(packageName.replace('.', '/'));
    while (resources.hasMoreElements()) {
        URL res = resources.nextElement();
        if (res.getProtocol().matches("(jar|zip)")) {
...
List>getClassesForPackage(String pckgname)
get Classes For Package
return getClassesForPackage(pckgname, true);
ArrayList>getClassesForPackage(String pckgname)
Attempts to list all the classes in the specified package as determined by the context class loader
final ArrayList<Class<?>> classes = new ArrayList<>();
try {
    final ClassLoader cld = Thread.currentThread().getContextClassLoader();
    if (cld == null) {
        throw new ClassNotFoundException("Can't get class loader.");
    final Enumeration<URL> resources = cld.getResources(pckgname.replace('.', '/'));
    URLConnection connection;
...
ListgetClassesInPackage(String packageName)
Given a package name, attempts to reflect to find all classes within the package on the local file system.
List<Class> classes = new ArrayList<>();
String packageNameSlashed = "/" + packageName.replace(".", "/");
URL directoryURL = Thread.currentThread().getContextClassLoader().getResource(packageNameSlashed);
if (directoryURL == null) {
    System.out.println("Could not retrieve URL resource: " + packageNameSlashed);
    return classes;
String directoryString = directoryURL.getFile();
...