Here you can find the source of getPath(String name, Class> relativeTo)
public static String getPath(String name, Class<?> relativeTo)
//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); } } }