Here you can find the source of getClassPath(Class> c)
public static URL getClassPath(Class<?> c)
//package com.java2s; //License from project: LGPL import java.net.MalformedURLException; import java.net.URL; import java.net.URLDecoder; public class Main { public static URL getClassPath(Class<?> c) { String classFile = c.getName().replace('.', '/') + ".class"; URL url = c.getClassLoader().getResource(classFile); if (url == null) return null; if (url.getProtocol().equals("jar")) { String urlFile = url.getFile(); int i = urlFile.indexOf("!"); if (i > 0) { try { URL jarURL = new URL(URLDecoder.decode(urlFile.substring(0, i), "UTF-8")); return jarURL; } catch (Exception ex) { ex.printStackTrace(); }//ww w . j ava 2 s . c o m } } String urlString = url.toString(); if (urlString.endsWith(classFile)) { try { return new URL(urlString.substring(0, urlString.length() - classFile.length())); } catch (MalformedURLException e) { e.printStackTrace(); } } return null; } }