I'm not sure exactly what you want to achieve. You can simply remove the end portion of the path using regex. I have used the String.class as an example.
public static void main(final String[] args) throws Exception {
// get URI of class
String clazz = "String.class";
URI resourceURI = String.class.getResource(clazz).toURI();
System.out.println(resourceURI);
System.out.println(resourceURI.toString().replaceAll("^(.+?)!?/?" + clazz + "$", "$1"));
clazz = "/lang/String.class";
System.out.println(resourceURI.toString().replaceAll("^(.+?)!?/?" + clazz + "$", "$1"));
clazz = "java/lang/String.class";
System.out.println(resourceURI.toString().replaceAll("^(.+?)!?/?" + clazz + "$", "$1"));
}
If you want to try and load the data into a Path then you can do the following:
public static void main(final String[] args) throws Exception {
// get URI of class
String clazz = "String.class";
URI resourceURI = String.class.getResource(clazz).toURI();
System.out.println(resourceURI.getScheme());
System.out.println(resourceURI.getRawSchemeSpecificPart());
URI fileURI = new URI(resourceURI.getRawSchemeSpecificPart());
System.out.println(fileURI.getScheme());
System.out.println(fileURI.getRawSchemeSpecificPart());
Path path = Paths.get(fileURI);
System.out.println(path);
System.out.println(path.getParent());
}