Java Class Path getPath(String name, Class relativeTo)

Here you can find the source of getPath(String name, Class relativeTo)

Description

get Path

License

Open Source License

Declaration

public static String getPath(String name, Class<?> relativeTo) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2008 The University of York.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * /*  w w  w . ja v  a  2  s  .  co m*/
 * Contributors:
 *     Dimitrios Kolovos - initial API and implementation
 ******************************************************************************/

import java.io.File;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

public class Main {
    public static String getPath(String name, Class<?> relativeTo) {
        return getFile(name, relativeTo).getAbsolutePath();
    }

    public static String getAbsolutePath(String basePath, String relativePath) {
        File file = new File(relativePath);
        if (!file.isAbsolute()) {
            file = new File(basePath, relativePath);
        }
        return file.getAbsolutePath();
    }

    public static File getFile(String name, Class<?> relativeTo) {
        try {
            final File clazz = new File(URLDecoder
                    .decode(relativeTo.getResource(relativeTo.getSimpleName() + ".class").getFile(), "UTF-8"));

            return new File(clazz.getParentFile(), name);

        } catch (UnsupportedEncodingException e) {
            throw new IllegalArgumentException(name + " could not be located relative to " + relativeTo);
        }
    }
}

Related

  1. getFullPathOfClass(Class cls)
  2. getFullpathRessources(Class c, String ressource)
  3. getInputStream(Class cls, String path)
  4. getPath(Class tClass)
  5. getPath(final String aPath, final Class aClass)
  6. getProgramRootPath(Class clazz)
  7. getRootAbsolutePathname(final Class clazz)
  8. getRunClassAllPath(Object obj)
  9. getSystemLibraryPath(Class locatorClass, String subDir)