Java ClassPath Get getClasspathFilePathFromName(String fileName)

Here you can find the source of getClasspathFilePathFromName(String fileName)

Description

Gets the absolute path to the specified file on the classpath.

License

Apache License

Parameter

Parameter Description
fileName The name of the file on the classpath to get the path to.

Exception

Parameter Description
FileNotFoundException If the file couldn't be found on the classpath.
URISyntaxException If the file's URI is not valid.

Return

The path to the file, including the file name.

Declaration

public static String getClasspathFilePathFromName(String fileName)
        throws FileNotFoundException, URISyntaxException 

Method Source Code

//package com.java2s;
/**/*from w  w  w .j  av a 2 s  . c o m*/
 * Copyright 2000-2012 TrackMate
 *
 * 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.io.FileNotFoundException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

public class Main {
    /**
     * Gets the absolute path to the specified file on the classpath.
     * @param fileName The name of the file on the classpath to get the path to.
     * @return The path to the file, including the file name.
     * @throws FileNotFoundException If the file couldn't be found on the classpath.
     * @throws URISyntaxException If the file's URI is not valid.
     */
    public static String getClasspathFilePathFromName(String fileName)
            throws FileNotFoundException, URISyntaxException {
        URL fileURL = Thread.currentThread().getContextClassLoader().getResource(fileName);
        if (fileURL == null) {
            throw new FileNotFoundException("Failed to find file on classpath: " + fileName);
        }
        URI fileURI = fileURL.toURI();
        File file = new File(fileURI);
        return file.getAbsolutePath();
    }
}

Related

  1. getClassPathFile(Class clazz)
  2. getClasspathFile(final String name)
  3. getClassPathFile(final String path, final ClassLoader classLoader)
  4. getClasspathFile(String fn)
  5. getClassPathFile(String pathName)
  6. getClassPathFor(final Class clazz)
  7. getClasspathForClass(Class targetClass)
  8. getClassPathFromString(String classpath)
  9. getClassPathPath()