Java Resource Get getResource(String filename)

Here you can find the source of getResource(String filename)

Description

Returns a url to a resource.

License

Open Source License

Declaration

public static URL getResource(String filename) 

Method Source Code


//package com.java2s;
/*/*  w w  w .  j a va  2 s .  co m*/
 *  StatCvs-XML - XML output for StatCvs.
 *
 *  Copyright by Steffen Pingel, Tammo van Lessen.
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  version 2 as published by the Free Software Foundation.
 *
 *  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

import java.io.File;

import java.net.MalformedURLException;
import java.net.URL;

public class Main {
    /**
     * Returns a url to a resource. The classpath is searched first,
     * then the file system is searched.
     */
    public static URL getResource(String filename) {
        URL url = ClassLoader.getSystemResource(filename);
        if (url == null) {
            url = Main.class.getResource(filename);
            if (url == null) {
                try {
                    url = new File(filename).toURL();
                } catch (MalformedURLException e) {
                    return null;
                }
            }
        }
        return url;
    }
}

Related

  1. getResource(final String name)
  2. getResource(final String res)
  3. getResource(final String resource)
  4. getResource(final String resourceName, final Class caller)
  5. getResource(String filename)
  6. getResource(String folder, String name)
  7. getResource(String inResource)
  8. getResource(String key, String bundleName, Locale locale)
  9. getResource(String name)