Here you can find the source of getResource(String path)
Parameter | Description |
---|---|
path | The classpath of the resource. |
Parameter | Description |
---|---|
UsageException | When the provided path does not translate to a valid resource. |
public static URL getResource(String path)
//package com.java2s; // Licensed under the Apache License, Version 2.0 (the "License"); import java.net.URL; public class Main { /**//from w w w. jav a 2 s.c om * Loads the resource from the class loader returned by {@link #getClassLoader()}. * * @param path * The classpath of the resource. * @return The URL of the resource identified by the provided path. * @throws UsageException * When the provided path does not translate to a valid resource. * @see ClassLoader#getResource(java.lang.String) */ public static URL getResource(String path) { URL url = getClassLoader().getResource(path); if (url == null) throw new IllegalArgumentException("Unable to find resource at " + path); return url; } /** * @return Returns the current thread's context class loader; when not found it returns the system class loader. */ public static ClassLoader getClassLoader() { ClassLoader loader = Thread.currentThread().getContextClassLoader(); return loader == null ? ClassLoader.getSystemClassLoader() : loader; } /** * @return Returns the class loader of the provided class; when not found it returns the class loader from * {@link #getClassLoader()}. */ public static ClassLoader getClassLoader(Class<?> clazz) { ClassLoader loader = clazz.getClassLoader(); return loader == null ? getClassLoader() : loader; } }