get Classpath Resource Image - Java java.util

Java examples for java.util:ResourceBundle

Description

get Classpath Resource Image

Demo Code


import org.apache.log4j.Logger;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;

public class Main{
    public static void main(String[] argv) throws Exception{
        String resourcePath = "java2s.com";
        System.out.println(getClasspathResourceImage(resourcePath));
    }//from w  ww  . j  a  v  a  2 s  .  c o  m
    private static final Logger logger = Logger
            .getLogger(ClassLoaderHelper.class);
    private static final ClassLoader classLoader = ClassLoaderHelper.class
            .getClassLoader();
    public static BufferedImage getClasspathResourceImage(
            String resourcePath) {
        InputStream inputStream = classLoader
                .getResourceAsStream(resourcePath);
        try {
            return ImageIO.read(inputStream);
        } catch (IOException e) {
            logger.warn("Unable to read image from classpath resource: "
                    + resourcePath, e);
        }
        return null;
    }
}

Related Tutorials