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

PropertiesloadForClass(final Class clazz, final String filename)
load For Class
final Properties props = new Properties();
final InputStream in = clazz.getClassLoader().getResourceAsStream(filename);
if (in != null) {
    try {
        props.load(in);
    } catch (IOException e) {
        e.printStackTrace();
        props.put("stamp", "loading from " + filename + "failed: " + e);
...
ManifestloadManifest(Class manifestFileClass)
load Manifest
try {
    URL jarFileLocation = manifestFileClass.getClassLoader()
            .getResource(manifestFileClass.getName().replace(".", "/") + ".class");
    if (jarFileLocation != null) {
        String jarFilePath = jarFileLocation.getFile();
        jarFilePath = jarFilePath.substring(0, jarFilePath.indexOf("!"));
        if (jarFilePath.startsWith("file:")) {
            jarFilePath = jarFilePath.substring(jarFilePath.indexOf(":") + 1);
...
ManifestloadManifest(Class theClass)
load Manifest
URI uri = theClass.getProtectionDomain().getCodeSource().getLocation().toURI();
File file = new File(uri.getPath());
URL url = file.isDirectory() ? uri.resolve(MANIFEST_FILE).toURL()
        : new URL("jar:" + uri.toASCIIString() + "!/" + MANIFEST_FILE); 
try (InputStream in = setupConnection(url).getInputStream()) {
    return new Manifest(in);
ManifestloadManifestFrom(Class c)
load Manifest From
URLClassLoader cl = (URLClassLoader) c.getClassLoader();
URL url = cl.findResource("META-INF/MANIFEST.MF");
Manifest manifest = new Manifest(url.openStream());
return manifest;
voidloadProperties(final Properties properties, final String fileName, final ClassLoader cl)
Load properties file fileName and put it properties into properties instance.
Enumeration<URL> urlProperties = null;
try {
    urlProperties = cl == null ? ClassLoader.getSystemResources(fileName) : cl.getResources(fileName);
} catch (IOException e) {
    return;
Properties fileProperties = new Properties();
while (urlProperties.hasMoreElements()) {
...
StringloadStyleSheet(Class type)
load Style Sheet
String name = type.getSimpleName();
URL url = type.getResource(name);
if (url == null)
    url = type.getResource(name + ".css");
return url.toExternalForm();
StringloadTextFile(Class relToThisClass, String relFileName)
load a text from a text file from a class relative path.
URL url = getURL(relToThisClass, relFileName);
String eol = "\n"; 
String text;
if (url != null) {
    StringBuffer buffer = new StringBuffer();
    InputStream input = url.openStream();
    BufferedReader reader = new BufferedReader(new InputStreamReader(input));
    String line;
...
StringpathFromLoaders(final Class clazz)
Walks class loader hierarchy of clazz looking for instances of URLClassLoader.
final TreeSet sysPths = new TreeSet();
gatherSystemPaths(sysPths);
final StringBuilder sbuf = new StringBuilder();
final LinkedList urlLst = new LinkedList();
for (ClassLoader cl = clazz.getClassLoader(); cl != null; cl = cl.getParent()) {
    if (!(cl instanceof java.net.URLClassLoader)) {
        continue;
    final java.net.URLClassLoader ucl = (java.net.URLClassLoader) cl;
    final java.net.URL[] urls = ucl.getURLs();
    if (urls == null)
        continue;
    for (int i = urls.length - 1; i > -1; i--) {
        final java.net.URL url = urls[i];
        final String prot = url.getProtocol();
        if (!"file".equalsIgnoreCase(prot))
            continue;
        urlLst.addFirst(url);
for (final Iterator itr = urlLst.iterator(); itr.hasNext();) {
    final java.net.URL url = (java.net.URL) itr.next();
    final String fnam = url.getFile();
    final File fil = new File(fnam);
    if (sysPths.contains(fil))
        continue;
    if (!fil.exists()) {
        final String unEncNam = URLDecoder.decode(fnam, "utf-8");
        final File unEncFil = new File(unEncNam);
        if (unEncFil.exists())
            sbuf.append(unEncNam);
    } else {
        sbuf.append(fnam);
    sbuf.append(File.pathSeparatorChar);
final String ret = sbuf.toString();
return ret;
voidprint(ClassLoader loader)
print
System.out.println("Dump classload: " + loader.toString());
URL[] urls = getClassLoaderURLs(loader);
for (int i = 0; urls != null && i < urls.length; i++) {
    System.out.println(urls[i].toString());
System.out.println("End of classload: " + loader.toString());
StringreadAll(ClassLoader cl, String path)
read All
FileReader reader = null;
try {
    URL url = cl.getResource(path);
    if (url == null)
        throw new NullPointerException("Url not found");
    File file = new File(url.getFile());
    reader = new FileReader(file);
    char[] buffer = new char[1024];
...