When working with Java, you might encounter the `java.lang.IllegalArgumentException: URI is not absolute` error. This error occurs when you are trying to create a `URI` object with a string that doesn't represent an absolute URI. This comprehensive guide will help you understand the causes of this error and provide step-by-step solutions to fix it.
## Table of Contents
1. [Understanding URIs](#understanding-uris)
2. [Causes of the Error](#causes-of-the-error)
3. [Solutions](#solutions)
1. [Using Absolute URIs](#using-absolute-uris)
2. [Handling Relative URIs](#handling-relative-uris)
4. [FAQ](#faq)
5. [Related Links](#related-links)
## Understanding URIs
A Uniform Resource Identifier (URI) is a string of characters that identifies a name or a resource on the internet. URIs can be of two types:
1. Absolute URI: Contains a scheme (such as `http`, `https`, `file`, etc.) followed by a colon and a hierarchical part.
Example: `https://example.com/test`
2. Relative URI: Doesn't contain a scheme and is usually relative to a base URI.
Example: `/test`
Learn more about URIs in the [official Java URI documentation](https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java.base/java/net/URI.html).
## Causes of the Error
The `java.lang.IllegalArgumentException: URI is not absolute` error occurs when you try to create a `URI` object with a string that doesn't represent an absolute URI.
For example, the following code will throw an error:
```java
import java.net.URI;
public class Main {
public static void main(String[] args) {
String uriString = "/test";
URI uri = URI.create(uriString);
}
}
Solutions
Using Absolute URIs
Ensure that you are using an absolute URI when creating a URI
object. An absolute URI contains the scheme and the hierarchical part.
Example:
import java.net.URI;
public class Main {
public static void main(String[] args) {
String uriString = "https://example.com/test";
URI uri = URI.create(uriString);
}
}
Handling Relative URIs
If you need to work with relative URIs, you can use the java.net.URL
class to handle them. The URL
class allows you to specify a base URL to resolve the relative URI against.
Example:
import java.net.URL;
public class Main {
public static void main(String[] args) {
try {
URL baseUrl = new URL("https://example.com");
URL relativeUrl = new URL(baseUrl, "/test");
} catch (Exception e) {
System.err.println(e);
}
}
}
FAQ
Q1: Can I use the java.net.URI
class to handle relative URIs?
No, the java.net.URI
class doesn't support handling relative URIs. You need to use the java.net.URL
class to handle relative URIs.
Q2: How can I check if a URI is absolute in Java?
You can use the isAbsolute()
method of the java.net.URI
class to check if a URI is absolute.
Example:
URI uri = new URI("https://example.com/test");
System.out.println(uri.isAbsolute()); // Output: true
Q3: How can I convert a relative URI to an absolute URI in Java?
You can use the java.net.URL
class to convert a relative URI to an absolute URI.
Example:
URL baseUrl = new URL("https://example.com");
URL relativeUrl = new URL(baseUrl, "/test");
URI absoluteUri = relativeUrl.toURI();
Q4: How can I resolve a relative URI against a base URI in Java?
You can use the resolve()
method of the java.net.URI
class to resolve a relative URI against a base URI.
Example:
URI baseUri = new URI("https://example.com");
URI relativeUri = new URI("/test");
URI resolvedUri = baseUri.resolve(relativeUri);
Q5: Can I create a java.net.URI
object from a java.net.URL
object?
Yes, you can create a java.net.URI
object from a java.net.URL
object using the toURI()
method.
Example:
URL url = new URL("https://example.com/test");
URI uri = url.toURI();