Java Resource File runtimeResourceLocation(String src)

Here you can find the source of runtimeResourceLocation(String src)

Description

Attempts to resolve the src to a URL.

License

Open Source License

Parameter

Parameter Description
src src file path or qualified class name.

Return

resolved URL or null if none found.

Declaration

public static URL runtimeResourceLocation(String src) 

Method Source Code


//package com.java2s;
/*//from   w  ww.  j  a v  a  2 s .  co  m
 Copyright (C) 2003 EBI, GRL
    
 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.
    
 This library 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
 Lesser General Public License for more details.
    
 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.net.URL;

import com.mysql.jdbc.Driver;

public class Main {
    /**
     * Attempts to resolve the src to a URL.
     * 
     * This is useful for checking where a class or resource
     * file is being loaded from by the classloadeder. 
     * <ul>e.g. src = ...
     * <li>org.ensembl.Example</li>
     * <li>org/ensembl/Example.class</li>
     * <li>com.mysql.jdbc.Driver</li>
     * </ul>
     * 
     * Performs some magic to try to resolve the name if necessary,
     * e.g. for org.ensembl.Example "full names" like org/ensembl/Example.class
     * and org\ensembl\Example.class will be tried.
     * @param src src file path or qualified class name.
     * @return resolved URL or null if none found.
     */
    public static URL runtimeResourceLocation(String src) {

        URL url = null;

        // String.replaceAll gets confused if sep == "\\" 
        // (e.g. windows file separator) so we need to 'expand it'
        String[] variants = { src, src + ".class", src.replaceAll("\\.", "\\\\"),
                src.replaceAll("\\.", "\\\\") + ".class", src.replaceAll("\\.", "/"),
                src.replaceAll("\\.", "/") + ".class", };
        for (int i = 0; url == null && i < variants.length; i++)
            url = Driver.class.getClassLoader().getResource(variants[i]);

        return url;
    }
}

Related

  1. openInputStream(String resourceString, ClassLoader classLoader)
  2. parseASX(String inputResource)
  3. printUsage(ResourceBundle bundle)
  4. put(String resource, Map headers, String data)
  5. resourceExists(String s)
  6. sanitizeResource(String inputResource)
  7. toFile(Class c, String resource)
  8. toStream(String resource)
  9. unpackDeps(final String resource, final ClassLoader cl)