Java Resource Path Get getRelativeResource(Class clazz, String relativePath)

Here you can find the source of getRelativeResource(Class clazz, String relativePath)

Description

get Relative Resource

License

Apache License

Declaration

public static File getRelativeResource(Class<?> clazz,
            String relativePath) 

Method Source Code

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

import java.io.File;

import java.net.URL;

public class Main {
    public static File getRelativeResource(Class<?> clazz,
            String relativePath) {
        String packageName = clazz.getPackage().getName();
        String packagePath = packageName.replaceAll("\\.", "/");
        String wholePath = packagePath + "/" + relativePath;

        URL url = Thread.currentThread().getContextClassLoader()
                .getResource(wholePath);

        if (url == null) {
            String message = String
                    .format("File [%s] not found", wholePath);
            throw new IllegalArgumentException(message);
        }/*from  ww  w  .j a  v  a2 s .  c o m*/

        return new File(url.getPath());
    }
}

Related

  1. getFileFromResource(Class clazz, String path)
  2. getFileResourcePaths(final Class clazz, final String fileNameRegex)
  3. getPath(final Object resource)
  4. getPathOfResource(Class clazz, String fileName)
  5. getPluginResource(Bundle bundle, String resourcePath)
  6. getResource(Class cl, String path)
  7. getResource(final Class baseClass, final String path)
  8. getResource(final String path)
  9. getResource(String path)