public static File getResourceRoot ( final URL url , final String resource ) { String path = null ; if ( url != null ) { final String spec = url . toExternalForm ( ) ; if ( ( JAR_FILE ) . regionMatches ( true , 0 , spec , 0 , JAR_FILE . length ( ) ) ) { URL jar ; try { jar = new URL ( spec . substring ( JAR . length ( ) , spec . lastIndexOf ( "!/" ) ) ) ; } catch ( final MalformedURLException e ) { throw new IllegalArgumentException ( "Invalid JAR URL: " + url + ", " + e . getMessage ( ) ) ; } path = decodeUrl ( jar . getPath ( ) ) ; } else if ( FILE . regionMatches ( true , 0 , spec , 0 , FILE . length ( ) ) ) { path = decodeUrl ( url . getPath ( ) ) ; path = path . substring ( 0 , path . length ( ) - resource . length ( ) ) ; } else { throw new IllegalArgumentException ( "Invalid class path URL: " + url ) ; } } return path != null ? new File ( path ) : null ; }
Gets the absolute filesystem path to the class path root for the specified resource . The root is either a JAR file or a directory with loose class files . If the URL does not use a supported protocol an exception will be thrown .