public static String findPathJar ( final Class clas ) throws IllegalStateException { URL url ; String extURL ; try { url = clas . getProtectionDomain ( ) . getCodeSource ( ) . getLocation ( ) ; } catch ( final SecurityException ex ) { url = clas . getResource ( clas . getSimpleName ( ) + ".class" ) ; } extURL = url . toExternalForm ( ) ; try { url = new URL ( extURL ) ; } catch ( final MalformedURLException mux ) { // leave url unchanged; probably does not happen } try { return new File ( url . toURI ( ) ) . toString ( ) ; } catch ( final Exception ex ) { return new File ( url . getPath ( ) ) . toString ( ) ; } }
If the provided class has been loaded from a jar file that is on the local file system will find the absolute path to that jar file .