Java Resource Get loadResource(Class contextClass, String resourceName)

Here you can find the source of loadResource(Class contextClass, String resourceName)

Description

Returns the url of a resource.

License

Apache License

Parameter

Parameter Description
contextClass a parameter
resourceName a parameter

Declaration

public static URL loadResource(Class<?> contextClass, String resourceName) 

Method Source Code

//package com.java2s;
/**//from w  w w . j a va  2  s . co m
 * Licensed to jclouds, Inc. (jclouds) under one or more
 * contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  jclouds licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.net.URL;
import com.google.common.io.Resources;

public class Main {
    /**
     * Returns the url of a resource.
     * 1. The context class is being used.
     * 2. The thread context class loader is being used.
     * If both approach fail, returns null.
     *
     * @param contextClass
     * @param resourceName
     * @return
     */
    public static URL loadResource(Class<?> contextClass, String resourceName) {
        URL url = null;
        if (contextClass != null) {
            url = Resources.getResource(contextClass, resourceName);
        }
        if (url == null && Thread.currentThread().getContextClassLoader() != null) {
            url = Thread.currentThread().getContextClassLoader().getResource(resourceName);
        }
        return url;
    }
}

Related

  1. getResources(String pkgName)
  2. getResourcesFromDirectory(File resource, Pattern pattern)
  3. getResourceString(ResourceBundle rb, String key, Object param1)
  4. getResourceString(String key)
  5. getResourceString(String key, Object... args)
  6. loadResource(final String name)
  7. loadResource(final String s)
  8. readResource(String resource, Class c)
  9. readToString(final Class nearClass, final String resource)