1 package com.ochafik.lang; 2 3 import java.net.MalformedURLException; 4 import java.net.URL; 5 import java.net.URLDecoder; 6 7 public class ClassUtils { 8 public static URL getClassPath(Class<?> c) { 9 String classFile = c.getName().replace('.', '/') + ".class"; 10 URL url = c.getClassLoader().getResource(classFile); 11 if (url == null) 12 return null; 13 14 if (url.getProtocol().equals("jar")) { 15 String urlFile = url.getFile(); 16 int i = urlFile.indexOf("!"); 17 if (i > 0) { 18 try { 19 URL jarURL = new URL(URLDecoder.decode(urlFile.substring(0, i), "UTF-8")); 20 return jarURL; 21 } catch (Exception ex) { 22 ex.printStackTrace(); 23 } 24 } 25 } 26 String urlString = url.toString(); 27 if (urlString.endsWith(classFile)) { 28 try { 29 return new URL(urlString.substring(0, urlString.length() - classFile.length())); 30 } catch (MalformedURLException e) { 31 e.printStackTrace(); 32 } 33 } 34 return null; 35 } 36 }