Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URI path not absolute exception java(not android)

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=)

like image 324
Jürgen K. Avatar asked Sep 25 '15 10:09

Jürgen K.


People also ask

How do I fix Java Lang Illegalargumentexception URI is not absolute?

Solution 1If a relative url is sent to a rest call in the RestTemplate, the relative url has to be changed to an absolute url. The RestTemplate will use the absolute url to identify the resource. In the example below, the complete url “http:/localhost:8080/student” is used to invoke a rest call using RestTemplate.

What does it mean the URI is not absolute?

May 18, 2022•Knowledge 000127381 The error URI is not absolute as seen when the protocol (HTTP/HTTPs) is not defined for the URI in properties. This issue is caused due to the missing URI protocol http/https in any URI property of the configuration.

How to give absolute file path in java?

File getAbsolutePath() method in Java with Examples The getAbsolutePath() method is a part of File class. This function returns the absolute pathname of the given file object. If the pathname of the file object is absolute then it simply returns the path of the current file object.

How do you find the absolute path of URI?

Uri uri = data. getData(); File file = new File(uri. getPath());//create path from uri final String[] split = file. getPath().


1 Answers

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";
like image 68
Stanislav Avatar answered Sep 30 '22 14:09

Stanislav