Here you can find the source of getClasspathForClass(Class> targetClass)
public static File getClasspathForClass(Class<?> targetClass)
//package com.java2s; //License from project: Apache License import java.io.File; import java.net.URI; import java.net.URISyntaxException; public class Main { public static File getClasspathForClass(Class<?> targetClass) { URI location = null;/* w w w .j a va 2s . co m*/ try { location = targetClass.getProtectionDomain().getCodeSource().getLocation().toURI(); } catch (URISyntaxException e) { throw new RuntimeException(e); } if (!location.getScheme().equals("file")) { throw new RuntimeException(String.format("Cannot determine classpath for %s from codebase '%s'.", targetClass.getName(), location)); } return new File(location.getPath()); } }