Java Resource Get getResource(String name)

Here you can find the source of getResource(String name)

Description

Get URL name of resource and check if it exists somewhere in the classpath.

License

LGPL

Parameter

Parameter Description
name Name of the resource

Exception

Parameter Description
IOException if resource can't be found

Return

instance of an input stream or null if the resource couldn't be found

Declaration

public static URL getResource(String name) throws IOException 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.io.IOException;

import java.net.URL;

public class Main {
    /**/*ww w .j a va  2s .c  o  m*/
     * Get URL name of resource and check if it exists somewhere in the classpath.
     * @param name Name of the resource
     * @return instance of an input stream or <code>null</code> if the resource couldn't be found
     * @throws IOException if resource can't be found
     */
    public static URL getResource(String name) throws IOException {
        URL url = Thread.currentThread().getContextClassLoader().getResource(name);
        if (url == null) {
            throw new IOException("could not find " + name + " in classpath");
        }

        return url;
    }

    /**
     * Get URL of a resource from a given <code>folder</code>.
     * 
     * @see #getResource(String)
     */
    public static URL getResource(String folder, String name) throws IOException {
        return getResource(folder + "/" + name);
    }
}

Related

  1. getResource(String name)
  2. getResource(String name)
  3. getResource(String name)
  4. getResource(String name)
  5. getResource(String name)
  6. getResource(String name)
  7. getResource(String name)
  8. getResource(String name, Class neighbor)
  9. getResource(String name, Class caller)