Java ClassPath Get getClassPathFile(final String path, final ClassLoader classLoader)

Here you can find the source of getClassPathFile(final String path, final ClassLoader classLoader)

Description

Gets a File from a String that represents file path which is relative to current Class Path

License

Apache License

Parameter

Parameter Description
path Path to File
classLoader ClassLoader that should be able to see the file

Return

object or null if nothing is found

Declaration

public static File getClassPathFile(final String path, final ClassLoader classLoader) 

Method Source Code

//package com.java2s;
/*// w  w  w.j  a  v  a 2s  . co  m
 * Copyright 2009 Tomas Varaneckas 
 * http://www.varaneckas.com
 *
 * Licensed 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.io.File;

import java.net.URL;

public class Main {
    /**
     * Gets a {@link File} from a String that represents file path which is 
     * relative to current Class Path
     * 
     * @param path Path to File
     * @param classLoader {@link ClassLoader} that should be able to see the 
     *         file
     * @return {@link File} object or null if nothing is found
     */
    public static File getClassPathFile(final String path, final ClassLoader classLoader) {
        final URL url = classLoader.getResource(path);
        if (url == null) {
            return null;
        }
        return new File(url.getFile());
    }

    /**
     * Gets a {@link File} form a String that represents file path which is
     * relative to current Class Path. Uses system's default ClassLoader.
     * 
     * @see #getClassPathFile(String, ClassLoader)
     * @param path Path to File
     * @return {@link File} object or null if nothing is found
     */
    public static File getClassPathFile(final String path) {
        return getClassPathFile(path, ClassLoader.getSystemClassLoader());
    }
}

Related

  1. getClassPathAsString(ClassLoader classLoader)
  2. getClasspathDir(Class klass)
  3. getClasspathFile(Class clz)
  4. getClassPathFile(Class clazz)
  5. getClasspathFile(final String name)
  6. getClasspathFile(String fn)
  7. getClassPathFile(String pathName)
  8. getClasspathFilePathFromName(String fileName)
  9. getClassPathFor(final Class clazz)