001 package com.ochafik.lang; 002 003 import java.net.MalformedURLException; 004 import java.net.URL; 005 import java.net.URLDecoder; 006 007 public class ClassUtils { 008 public static URL getClassPath(Class<?> c) { 009 String classFile = c.getName().replace('.', '/') + ".class"; 010 URL url = c.getClassLoader().getResource(classFile); 011 if (url == null) 012 return null; 013 014 if (url.getProtocol().equals("jar")) { 015 String urlFile = url.getFile(); 016 int i = urlFile.indexOf("!"); 017 if (i > 0) { 018 try { 019 URL jarURL = new URL(URLDecoder.decode(urlFile.substring(0, i), "UTF-8")); 020 return jarURL; 021 } catch (Exception ex) { 022 ex.printStackTrace(); 023 } 024 } 025 } 026 String urlString = url.toString(); 027 if (urlString.endsWith(classFile)) { 028 try { 029 return new URL(urlString.substring(0, urlString.length() - classFile.length())); 030 } catch (MalformedURLException e) { 031 e.printStackTrace(); 032 } 033 } 034 return null; 035 } 036 }