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>getClassesInPackage(String packageName)
get Classes In Package
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
List<Class<?>> classes = new ArrayList<Class<?>>();
URL packageURL;
packageURL = classLoader.getResource(packageName.replace(".", "/"));
if (packageURL.getProtocol().equals("jar")) {
    packageName = packageName.replace(".", "/");
    String jarFileName;
    JarFile jf;
...
List>getClassesInPackage(String packageName)
Retrieves recursively all the classes belonging to a package.
return getClassesInPackage(packageName, Thread.currentThread().getContextClassLoader());
Class[]getClassesInPackage(String pckgname)
get Classes In Package
ArrayList<Class> classes = new ArrayList<>();
ClassLoader cld = Thread.currentThread().getContextClassLoader();
if (cld == null) {
    throw new ClassNotFoundException("Can't get class loader.");
String path = pckgname.replace('.', '/');
Enumeration<URL> resources = null;
try {
...
Class[]getClassesInPackage(String targetPackage)
Returns an array of classes within a target package
List<Class> classList = new ArrayList<Class>();
URL packageResource = Thread.currentThread().getContextClassLoader()
        .getResource(targetPackage.replace(".", "/").trim());
if (packageResource == null) {
    System.out.println("Could not create resource for package " + targetPackage);
    return null;
File packageDirectory = new File(packageResource.getFile());
...
ListgetClassNameByPackage(String packageName)
get Class Name By Package
List<String> fileNames = null;
ClassLoader loader = Thread.currentThread().getContextClassLoader();
String packagePath = packageName.replace(".", "/");
URL url = loader.getResource(packagePath);
if (url != null) {
    String type = url.getProtocol();
    if (type.equals("file")) {
        return fileNames = getClassNameByFile(packageName, URLDecoder.decode(url.getPath()), null);
...
ListgetClassNamesByPkg(String pkg)
get Class Names By Pkg
List<String> result = new ArrayList<>();
ClassLoader loader = Thread.currentThread().getContextClassLoader();
String packagePath = pkg.replace(".", "/");
URL url = loader.getResource(packagePath);
File dir = new File(url.getPath());
for (File f : dir.listFiles()) {
    result.add(f.getName().replace(".class", ""));
return result;
ListgetClassNamesForPackage(final Package p)
get Class Names For Package
final List<String> classes = new ArrayList<String>();
final String name = p.getName();
final String path = name.replace('.', '/');
final URL resource = ClassLoader.getSystemClassLoader().getResource(path);
if (resource.toString().startsWith("jar:")) {
    processJar(resource, name, classes);
} else {
    processDirectory(new File(resource.getPath()), name, classes);
...
ArrayListgetClassNamesFromPackage(String packageName)
get Class Names From Package
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
URL packageURL;
ArrayList<String> names = new ArrayList<String>();
packageName = packageName.replace(".", "/");
packageURL = classLoader.getResource(packageName);
try {
    URI uri = new URI(packageURL.toString());
    if (uri.getPath() == null) { 
...
ArrayListgetClassNamesFromPackage(String packageName)
get Class Names From Package
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
ArrayList<String> names = new ArrayList<String>();
packageName = packageName.replace(".", "/");
URL packageURL = classLoader.getResource(packageName);
URI uri = new URI(packageURL.toString());
File folder = new File(uri.getPath());
File[] files = folder.listFiles();
for (File file : files) {
...
ArrayListgetClassNamesFromPackage(String packageName)
get Class Names From Package
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
ArrayList<String> names = new ArrayList<String>();
packageName = packageName.replace(".", File.separator) + File.separator;
URL packageURL = classLoader.getResource(packageName);
if (packageURL.getProtocol().equals("jar")) {
    String jarFileName;
    JarFile jf;
    Enumeration<JarEntry> jarEntries;
...