Java Utililty Methods Class Loader

List of utility methods to do Class Loader

Description

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

Method

ClassLoadergetCustomClassloader(List entries)
get Custom Classloader
List<URL> urls = new ArrayList<URL>();
File file;
if (entries != null) {
    for (String classPathEntry : entries) {
        file = new File(classPathEntry);
        if (!file.exists()) {
            throw new RuntimeException(classPathEntry); 
        try {
            urls.add(file.toURI().toURL());
        } catch (MalformedURLException e) {
            throw new RuntimeException(classPathEntry); 
ClassLoader parent = Thread.currentThread().getContextClassLoader();
URLClassLoader ucl = new URLClassLoader(urls.toArray(new URL[urls.size()]), parent);
return ucl;
FilegetFileFromClassLoader(String fileName)
Helper method which gets a file using the current class loader.
URL url = Thread.currentThread().getContextClassLoader().getResource(fileName);
File file = null;
if (url != null) {
    file = new File(url.getFile());
return file;
ListgetFilesByPackgeName(String packageName, File _file, ClassLoader classLoader, List list)
get Files By Packge Name
_file = _file != null ? _file : getPackageFolder(packageName, classLoader);
if (_file != null) {
    File[] files = _file.listFiles();
    for (File file : files) {
        if (file.isDirectory()) {
            if (!file.getName().contains("."))
                getFilesByPackgeName(packageName + '.' + file.getName(), _file, classLoader, list);
        } else
...
URLClassLoadergetJvmExtClassLoader()
get Jvm Ext Class Loader
return (URLClassLoader) getAppClassLoader().getParent();
FilegetLoadingDir(Class clazz)
get Loading Dir
try {
    return new File(clazz.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
} catch (URISyntaxException e) {
    return null;
ManifestgetManifest(ClassLoader cl, String extension)
Returns the manifest of the jar which contains the specified extension.
try {
    Enumeration resources = cl.getResources("META-INF/MANIFEST.MF");
    while (resources.hasMoreElements()) {
        Manifest manifest = new Manifest(((URL) resources.nextElement()).openStream());
        Attributes attributes = manifest.getMainAttributes();
        if (attributes != null && extension.equals(attributes.getValue(Attributes.Name.EXTENSION_NAME))) {
            return manifest;
} catch (IOException ex) {
    throw new RuntimeException("Unable to read manifest.", ex);
return null;
FilegetPackageFolder(String packageName, ClassLoader classLoader)
get Package Folder
Enumeration<URL> resources = classLoader.getResources(packageName.replace('.', '/'));
if (resources.hasMoreElements())
    return new File(resources.nextElement().getFile());
return null;
URLClassLoadergetParentClassLoader()
get Parent Class Loader
return (URLClassLoader) ClassLoader.getSystemClassLoader();
ClassLoadergetProjectClassLoader(IJavaProject javaProject)
Returns the Eclipse Project class loader using project build path
IClasspathEntry[] entries = javaProject.getResolvedClasspath(true);
String wsPath = javaProject.getProject().getLocation().toPortableString();
String firstEntryLocation = javaProject.getPath().toPortableString();
int idx = wsPath.indexOf(firstEntryLocation);
URL[] urls = null;
int i = 0;
String output = javaProject.getOutputLocation().toPortableString();
urls = new URL[entries.length + 1];
...
ClassLoadergetProjectClassLoader(IJavaProject javaProject, ClassLoader parentClassLoader)
Creates a ClassLoader using the project's build path.
IProject project = javaProject.getProject();
IWorkspaceRoot root = project.getWorkspace().getRoot();
List<URL> urls = new ArrayList<>();
urls.add(
        new File(project.getLocation() + "/" + javaProject.getOutputLocation().removeFirstSegments(1) + "/") 
                .toURI().toURL());
for (IClasspathEntry classpathEntry : javaProject.getResolvedClasspath(true)) {
    if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
...