Importing Classes

Fully qualified names like java.net.URL are not the most convenient thing to have to type. You can use the shorter class names like URL without the java.net part if you first import the class by adding an import statement at the top of the file. For example,

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

public class URLSplitter {

  public static void main(String[] args) {
   
    for (int i = 0; i < args.length; i++) {
      try {
        URL u = new URL(args[i]);
        System.out.println("Protocol: " + u.getProtocol());
        System.out.println("Host: " + u.getHost());
        System.out.println("Port: " + u.getPort());
        System.out.println("File: " + u.getFile());
        System.out.println("Ref: " + u.getRef());
      }
      catch (MalformedURLException e) {
        System.err.println(args[i] + " is not a valid URL");
      }
    }
       
  }
}

Previous | Next | Top | Cafe au Lait

Copyright 1997, 1999 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified February 3, 1999