Java Resource Load getResourceUrl(String resourcePath)

Here you can find the source of getResourceUrl(String resourcePath)

Description

Returns the specified resource URL or null if not found.

License

Open Source License

Parameter

Parameter Description
resourcePath the resource path

Return

the resource URL

Declaration

public static URL getResourceUrl(String resourcePath) 

Method Source Code

//package com.java2s;

import java.net.URL;

public class Main {
    /**// www  . j  a  v  a  2s .  c  om
     * Returns the specified resource URL or null if not found.
     *
     * @param resourcePath the resource path
     * @return the resource URL
     */
    public static URL getResourceUrl(String resourcePath) {
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

        return classLoader.getResource(resourcePath);
    }

    /**
     * Returns the specified resource URL or null if not found.
     *
     * @param context the context path or package
     * @param resourceName the resource name
     * @return the resource URL
     * @see ResourceHelper#getResourcePath(Object, String)
     */
    public static URL getResourceUrl(Object context, String resourceName) {
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

        return classLoader.getResource(getResourcePath(context, resourceName));
    }

    /**
     * Returns the path of the resource defined by the specified relative
     * path and the specified context.
     * 
     * If the context is a path, it must be absolute and of the form
     * eg. com/mycompanie/myapp
     * If the context is a Class, its package name is used as a context path
     * eg. com.mycompanie.myapp.Main.class -> com/mycompanie/myapp
     * If the context is a Package object, its name is used as a context path
     * If the context is a regular object, its class package name is
     * used as context path.
     *
     * @param context the context path or package
     * @param resourcePath the resource path relative to the context
     * eg. image/hello.gif
     * @return the resource path
     * eg. com/mycompanie/myapp/image/hello.gif
     * 
     */
    public static String getResourcePath(Object context, String resourcePath) {
        if (context == null) {
            return resourcePath;
        }

        if (context instanceof String) {
            return context.toString() + '/' + resourcePath;
        }

        Package contextPackage = null;

        if (context instanceof Package) {
            contextPackage = (Package) context;
        } else if (context instanceof Class) {
            contextPackage = ((Class) context).getPackage();

        } else {
            contextPackage = context.getClass().getPackage();
        }

        return contextPackage.getName().replace('.', '/') + '/' + resourcePath;
    }
}

Related

  1. getResourceUri(final String packageToScan, final ClassLoader classLoader)
  2. getResourceUrl(final Class aClass, final String aPath)
  3. getResourceUrl(final String resourcePath)
  4. getResourceUrl(String path, String extension, ClassLoader loader)
  5. getResourceURL(String resourcePath)
  6. getResourceUsingFileStreams(InputStream source)
  7. getResponseCode(URI uri)