private static String findContainingJar ( Class my_class ) { ClassLoader loader = my_class . getClassLoader ( ) ; String class_file = my_class . getName ( ) . replaceAll ( "\\." , "/" ) + ".class" ; try { for ( Enumeration itr = loader . getResources ( class_file ) ; itr . hasMoreElements ( ) ; ) { URL url = ( URL ) itr . nextElement ( ) ; if ( "jar" . equals ( url . getProtocol ( ) ) ) { String toReturn = url . getPath ( ) ; if ( toReturn . startsWith ( "file:" ) ) { toReturn = toReturn . substring ( "file:" . length ( ) ) ; } toReturn = URLDecoder . decode ( toReturn , "UTF-8" ) ; return toReturn . replaceAll ( "!.*$" , "" ) ; } } } catch ( IOException e ) { throw new RuntimeException ( e ) ; } return null ; }
Find a jar that contains a class of the same name if any . It will return a jar file even if that is not the first thing on the class path that has a class with the same name .