Java Resource Load getResourceStreamFromClasspath(Class clazz, String resourceName)

Here you can find the source of getResourceStreamFromClasspath(Class clazz, String resourceName)

Description

Helper method for grabbing a file from the classpath and returning an InputStream

License

Open Source License

Parameter

Parameter Description
clazz a parameter
resourceName a parameter

Declaration

public static InputStream getResourceStreamFromClasspath(Class<?> clazz, String resourceName) 

Method Source Code

//package com.java2s;

import java.io.InputStream;

public class Main {
    /**//from  w ww  . ja  v a2s .  c om
     * Helper method for grabbing a file from the classpath and returning an InputStream
     * 
     * @param clazz
     * @param resourceName
     * @return
     */
    public static InputStream getResourceStreamFromClasspath(Class<?> clazz, String resourceName) {
        InputStream is = clazz.getResourceAsStream(resourceName);
        if (is == null) {
            is = clazz.getClassLoader().getResourceAsStream(resourceName);
            if (is == null)
                throw new RuntimeException("resource not found in classpath: " + resourceName + " class is: "
                        + clazz.getCanonicalName());
        }
        return is;
    }
}

Related

  1. getResourceStream(final String resource)
  2. getResourceStream(String className, ClassLoader classLoader)
  3. getResourceStream(String name)
  4. getResourceStream(String pathToResource)
  5. getResourceStream(String resource, Class root)
  6. getResourceStreamInPackage(Class clazz, String name)
  7. getResourceString(String filename)
  8. getResourceUri(final String packageToScan, final ClassLoader classLoader)
  9. getResourceUrl(final Class aClass, final String aPath)