URI path not absolute exception java(not android)

15,320

You are trying to create uniform resource identifier, but the name should follow the url conventions. This means, that it's necessary to provide a scheme (take a look here, to see all available schemes). So, in your case, you have to create the URI with string, like file:/pic.png or may be some other scheme.

As for your full path, it could be done like:

String string = "file:/C:/Users/Jurgen/newFile/myProject/pic.png";
Share:
15,320
Jürgen K.
Author by

Jürgen K.

Computer scientist

Updated on July 12, 2022

Comments

  • Jürgen K.
    Jürgen K. almost 2 years

    I have the following piece of source code

    import java.net.URI;
    import java.net.URISyntaxException;
    
        File file = new File("pic.png");
        BufferedImage image = ImageIO.read(file);
        String string = "pic.png";
     //the code works fine until here
        URI path = new URI(string);
        File f = new File(path);
        ColorProcessor image = new ColorProcessor(ImageIO.read(f));
    

    So the path that the File gets is correct. Image is buffered correctly also. Now my problem is that i'm getting the following exception

    Exception in thread "main" java.lang.IllegalArgumentException: URI is not absolute
        at java.io.File.<init>(Unknown Source)
    

    Why is my path not absolute?And how do i do it right?

    If i change the path like this:

    String string = "C:'\'Users'\'Jurgen'\'newFile'\'myProject'\'pic.png";
    

    Also tried like this

    String string = "C:/Users/Jurgen/newFile/myProject/pic.png";
    

    Then i get a new exception

    Exception in thread "main" java.lang.IllegalArgumentException: URI is not hierarchical
        at java.io.File.<init>(Unknown Source)
    

    P.S. not working with android packages for uri

    Thanks in advance=)