Java Resource Get getResource(final Class aClass, final String aName)

Here you can find the source of getResource(final Class aClass, final String aName)

Description

Returns the resource with the given name from the "datafiles"-directory.

License

Open Source License

Parameter

Parameter Description
aClass the class to which the resource should be located, cannot be <code>null</code>;
aName the resource name, including file extension, cannot be <code>null</code>.

Return

the URI pointing to the requested resource, never null .

Declaration

public static URL getResource(final Class<?> aClass, final String aName) 

Method Source Code


//package com.java2s;
/*//w w  w .  j av  a2  s . c  o m
 * OpenBench LogicSniffer / SUMP project 
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
 *
 * 
 * Copyright (C) 2010-2011 J.W. Janssen, www.lxtreme.nl
 */

import java.io.*;
import java.net.*;

public class Main {
    /**
     * Returns the resource with the given name from the "datafiles"-directory.
     * 
     * @param aClass
     *          the class to which the resource should be located, cannot be
     *          <code>null</code>;
     * @param aName
     *          the resource name, including file extension, cannot be
     *          <code>null</code>.
     * @return the URI pointing to the requested resource, never <code>null</code>
     *         .
     */
    public static URL getResource(final Class<?> aClass, final String aName) {
        return getResource(aClass, "datafiles", aName);
    }

    /**
     * Returns the resource with the given name from the given base directory.
     * 
     * @param aClass
     *          the class to which the resource should be located, cannot be
     *          <code>null</code>;
     * @param aBaseDir
     *          the base directory where the resource should be located, cannot be
     *          <code>null</code>;
     * @param aName
     *          the resource name, including file extension, cannot be
     *          <code>null</code>.
     * @return the URI pointing to the requested resource, never <code>null</code>
     *         .
     */
    public static URL getResource(final Class<?> aClass, final String aBaseDir, final String aName) {
        final URL resource = aClass.getClassLoader().getResource(aBaseDir + File.separator + aName);
        if (resource == null) {
            throw new RuntimeException("Resource not found: " + aName);
        }
        return resource;
    }
}

Related

  1. getResource(Class c, String name)
  2. getResource(Class clazz, String resource)
  3. getResource(Class controllerClass)
  4. getResource(Class testClass, String resource)
  5. getResource(final Class clazz, final String res)
  6. getResource(final String name)
  7. getResource(final String res)
  8. getResource(final String resource)
  9. getResource(final String resourceName, final Class caller)