Here you can find the source of getResourceUrl(String resourcePath)
Parameter | Description |
---|---|
resourcePath | the resource path |
public static URL getResourceUrl(String resourcePath)
//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; } }