Cut URI in relation to another

We have resolveone that converts a/b+ c/dto a/b/c/d.

We have relativizeone that converts a/b+ a/b/c/dto c/d.

Is there a way to convert a/b/c/d+ c/dto a/b?


For my special problem (classpaths), the URIs are not converted to java.nio.file.Paths, but the error

java.nio.file.InvalidPathException: Illegal char <:> at index 3: jar:file:/D:/devel/somejar.jar!/foo/Bar.class

I want to allow a recording directory (for example, a given Bar.class) and URIreceived from getClassLoader().getResource().toURI()to jar:file:/D:/devel/somejar.jar!/foo.

+4
source share
3 answers

You can use java.nio.file.Path, but you have to use a custom file system, since the URI scheme is jarnot here file. This page shows an example.

URI uri = URI.create("jar:file:/D:/devel/somejar.jar!/foo/Bar.class");

String[] array = uri.toString().split("!");
String jarFile = array[0];
String entryPath = array[1];
try(FileSystem fs = FileSystems.newFileSystem(URI.create(jarFile), new HashMap<>())) {
    Path path = fs.getPath(entryPath);
    URI parentUri = path.getParent().toUri();
    ...
}

:

URI uri = URI.create("jar:file:/D:/devel/somejar.jar!/foo/Bar.class");
URI parent = URI.create(uri.toString().substring(0, uri.toString().lastIndexOf("/")));
+1
URI uri = Main.class.getClassLoader().getResource("java/net/URISyntaxException.class").toURI();
//URI uri = URI.create("jar:file:/D:/devel/somejar.jar!/foo/Bar.class");
System.out.println(uri.toString());
System.out.println(Paths.get(uri.toString()).getParent());

: ! /Library/Java/JavaVirtualMachines/jdk 1.8.0_31.jdk/Contents/Home/jre/lib/rt.jar/java/net/URISyntaxException.class : !/Library/Java/JavaVirtualMachines/jdk1.8.0_31.jdk/Contents/Home/jre/lib/rt.jar/Java/

toString -

+1

I'm not sure exactly what you want to achieve. You can simply remove the end of the path with a regular expression. I used the String.class 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 loading data into the Path, 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());
}
0
source

Source: https://habr.com/ru/post/1627380/


All Articles