Java ClassPath getFileFromClasspath(final String fileName)

Here you can find the source of getFileFromClasspath(final String fileName)

Description

Load the resource either using current class loader or using parent class loader

License

Apache License

Parameter

Parameter Description
fileName a parameter

Declaration

public static File getFileFromClasspath(final String fileName) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.File;

import java.net.URL;

public class Main {
    /**// ww w  .j a  v  a  2  s . c o  m
     * Load the resource either using current class loader or using parent class
     * loader
     * 
     * @param fileName
     * @return
     */
    public static File getFileFromClasspath(final String fileName) {
        File file = null;

        // Load the resource using current class loader
        URL url = Thread.currentThread().getClass().getResource(fileName);

        if (url != null) {
            file = new File(url.getFile());
        } else {
            // load the resource using the parent context
            url = Thread.currentThread().getContextClassLoader().getResource(fileName);
            if (url != null) {
                file = new File(url.getFile());
            }
        }

        return file;
    }
}

Related

  1. findDirectoryFromClasspath(Class cls, String file)
  2. findResourceOnClasspath(String resourceName)
  3. getAvailableClassPathInfo(final ClassLoader classLoader)
  4. getBootstrapClassPath()
  5. getDriverClassLoader(String driverClasspath)
  6. getFileFromClasspath(String fileName)
  7. getFileFromClasspath(String name)
  8. getFileFromClasspathResource(String resource)
  9. getFilePathFromClasspath(String filename)