URL Creation

In this chapter you will learn:

  1. Creating a URL with a single string
  2. Creating a URL With components

Creating a URL with a single string

The following code creates a URL from single string.

import java.net.URL;
/*from  j a  va 2  s.c  om*/
public class Main {
  public static void main(String[] argv) throws Exception {

    URL url = new URL("http://java2s.com:80/index.html");
    System.out.println(url);
  }
}

The code above generates the following result.

Creating a URL With components

The following code creates a URL from each component passes in as different parameters.

import java.net.URL;
/*from  j  a v  a 2  s  .c om*/
public class Main {
  public static void main(String[] argv) throws Exception {

    URL url = new URL("http", "java2s.com", 80, "/index.html");
    System.out.println(url);
  }
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. URL to referencing jar file in the file system
  2. How to reference a single file in a jar file
Home » Java Tutorial » URL URI
URL
URL Creation
URL for jar file
URL Components
Convert file path to URL
URL Relative
URL Protocol
Read from URL
Compare URL
URLConnection
HTTP Header
URLConnection Post
Cookie
URLConnection Read
HttpURLConnection
HttpURLConnection Properties
HttpURLConnection proxy
HttpURLConnection Authenticator
HTTPS
JarURLConnection
Encode a URL
Decode a URL
URI
URI Normalization
URI Resolution
URI Relativization
Convert URI to URL
IP Address
IP Ping
NetworkInterface