How to Get URL Object with Relative Path in Java


In this example, let’s see how to create a URL object using relative path in Java.

Source Code

package com.beginner.examples;

import java.net.MalformedURLException;
import java.net.URL;

public class GetURLRelativePath {

	public static void main(String[] args) {
		
		try {
			
			URL testUrl = new URL("Http://www.test.Url");
			
			URL relativeUrl = new URL(testUrl,"/demo1/test1");
			
			System.out.println(relativeUrl.toString());
			
		} catch (MalformedURLException e) {
			
			e.printStackTrace();
		}
		
		
	}
}

Output:

http://www.test.Url/demo1/test1

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments