Java API Tutorial - Java URL(URL context, String spec) Constructor








Syntax

URL(URL context, String spec) constructor from URL has the following syntax.

public URL(URL context,  String spec) throws MalformedURLException

Example

In the following code shows how to use URL.URL(URL context, String spec) constructor.

import java.net.MalformedURLException;
import java.net.URL;
//from  w ww. ja v  a 2 s .c om
public class Main{

  public static void main(String[] a) {

    try {
      URL base = new URL("http://www.java2s.com/");
      URL relative = new URL(base, "index.html");
      System.out.println(relative);
    } catch (MalformedURLException ex) {
      ;
    }

  }

}

The code above generates the following result.