java.net Package

Last Updated : 6 Oct 2026

Java provides the java.net package for developing applications that communicate over a network. The package contains classes and interfaces for working with URLs, IP addresses, sockets, and other network-related operations.

In this chapter, you will learn about the java.net package and the commonly used classes and interfaces provided for network programming in Java.

What is java.net Package?

The java.net package provides classes and interfaces for implementing network applications in Java. These classes can be used to establish network connections, communicate between applications, work with Internet addresses, and access resources through URLs.

Java programs can use the classes provided by this package to perform various network operations without directly handling the low-level details of network communication.

Classes in java.net Package

The java.net package contains several classes for performing network-related operations. Some commonly used classes are discussed below.

ClassDescription
AuthenticatorFor networking applications it is first important to fetch the authentication for a connection in a network. The object of the Authenticator class is responsible for obtaining the same. The class is declared as follows:
public class Authenticator extends Object
The class methods are inherited from the Object class.
CacheRequestWe all know about the cache memory, which is used for fast retrieval of information. The class CacheRequest does somewhat similar work by storing the responses in the CacheResponse class. The class is declared as follows:
public abstract class CacheRequest extends Object
The class methods are inherited from the Object class.
CacheResponseThe class aims at the retrieval of records from the CacheResponse. The class is declared as follows:
public abstract class CacheResponse extends Object
The class methods are inherited from the Object class.
ContentHandlerThe connection in a network is established with the help of URL. The class ContentHandler is the superclass of the classes which are appointed for reading the objects from the URL. The class is declared as follows:
public abstract class ContentHandler extends Object
The class methods are inherited from the Object class.
CookieHandlerThe class object is implied for the association of HTTP protocol handler with the HTTP state management policy implementation. The class is declared as follows:
public abstract class CookieHandler extends Object
The class methods are inherited from the Object class.
CookieManagerThe class extends the CookieHandler class i.e. The CookieManager implements the CookieHnadler. The class is declared as follows:
public abstract class CookieManager extends CookieHandler
The class methods are inherited from the CookieHandler and Object class.
DatagramPacketA datagram packet is rendered by the class. The class is declared as follows:
public final class DatagramPacket extends Object
The class methods are inherited from the Object class
DatagramSocketFor the transfer of datagram packets in a network an entity called as socket is required. The class provides the same. The class is declared as follows:
public final class DatagramSocket extends Object implements Closeable
The class methods are inherited from the Object class
DatagramSocketImplThe class serves as the parent for the implementation of sockets and datagram. The class is declared as:
public final class DatagramSocketImp extends Object implements SocketOptions
The class methods are inherited from the Object class
InterfaceAddressThe address of the network interface is represented by this class. The class is declared as follows:
public class InterfaceAddress extends Object
The class methods are inherited from the Object class.
JarURLConnectionThe class is responsible for the establishment of the URL connection to the JAR files. The class is declared as follows:
public abstract class JarURLConnection extends URLConnection
The class methods are inherited from the URLConnection and Object class.
MulticastSocketFor the transfer of Multicast IP packets the class is implied. The class is declared as follows:
public abstract class MulticastSocket extends DatagramSocket.
The class methods are inherited from the DatagramSocket and Object class.
InetSocketAddressIP Socket Address is a combination of the IP address and the port number. The class provides the implementation of the same. It is declared as follows:
public class InetSocketAddress extends Object implements SocketAdress
The class methods are inherited from the Object class
InetAddressIP(Internet Protocol) address is represented by the class. The class is declared as:
public final class InetAddress extends Object implements Serializable
The class methods are inherited from the Object class.
Inet4AddressThe internet address is represented as the Internet Protocol version 6 i.e Inet6 address. The class is declared as:
public final class Inet6Address extends InetAddress
The class methods are inherited from the Object and InetAddress class.
Inet6AddressThe internet address is represented as the Internet Protocol version 4 i.e Inet4 address. The class is declared as:
public final class Inet4Address extends InetAddress
The class methods are inherited from the Object and InetAddress class.
IDNThe class is implied for the conversion of IDN between the ACE and Unicode representation. The class is declared as:
public final class IDN extends Object
The class methods are inherited from the Object class.
HttpURLConnectionThe class is implied for obtaining the URL connections and also has support for HTTP features. The class is declared as:
public final class HttpURLConnection extends URLConnection
The class methods are inherited from the Object and URLConnection class.
HttpCookieWe all have heard about cookies in the web browsers which are appointed for carrying of information related to states between the client and the server. The class represents such HTTP cookies. The class is declared as:
public final class HttpCookie extends Object implements Cloneable
The class methods are inherited from the Object class.
NetPermissionFor providing various network related permissions the class is used. The class is declared as:
public final class NetPermission extends BasePermission
The class methods are inherited from the BasePermission class.
NetworkInterfaceA network interface is representation is provided by the class. The class is declared as:
public final class NetworkInterface extends Object
The class methods are inherited from the Object class.
PasswordAuthenticationThe class is used by the Authenticator as a data holder. The class is declared as:
public final class PasswordAuthentication extends Object
The class methods are inherited from the Object class.
ProxyThe class is used for proxy related settings. The class is declared as:
public final class Proxy extends Object
The class methods are inherited from the Object class.
ProxySelectorThe class is used for the selection for the proxy server to be used. The class is declared as:
public final class ProxySelector extends Object
The class methods are inherited from the Object class.
ResponseCacheCaches which are linked to the URL connections are represented by this class. The class is declared as:
public abstract class ResponseCache extends Object
The class methods are inherited from the Object class.
SecureCacheResponseA cache which is obtained through the use of secure means is represented by this class. The class is declared as:
public abstract class SecureCacheResponse extends CacheResponse
The class methods are inherited from the Object class.
ServerSocketThe class is implied for the implementation of server sockets. The class is declared as:
public abstract class ServerSocket extends Object implements Cloaseable.
The class methods are inherited from the Object class.
SocketThe sockets which are linked to the client side is implemented by the class. The class is declared as:
public abstract class Socket extends Object implements Cloaseable.
The class methods are inherited from the Object class.
SocketAddressThe class is implied for the representation of Socket Address. The class is declared as:
public abstract class SocketAddress extends Object implements Serializable
The class methods are inherited from the Object class.
SocketImplThe class is a parent class of all the socket implementing classes. The class is declared as:
public abstract class SocketImpl extends Object implements SocketOptions
The class methods are inherited from the Object class.
SocketPermissionThe class is implied for the provision of network access with the help of sockets. The class is declared as:
public final class SocketPermission extends Permission implements Serializable
The class methods are inherited from the Object and Permission class.
StandardSocketOptionsAs implied by the class name, the class is used to provide the standard socket options. The class is declared as:
public abstract class StandardSocketOptions extends Object
The class methods are inherited from the Object class.
URIThe class is a representation of Uniform Resource Identifier. The class is declared as:
public final class URI extends Object implements Serializable, Comparable
The class methods are inherited from the Object class.
URLThe class is a representation of Uniform Resource Locator. The class is declared as:
public final class URL extends Object implements Serializable
The class methods are inherited from the Object class.
URLClassLoaderThe class is used for loading of loading of various classes and resources specified with the search path. The class is declared as:
public final class URL extends SecureClassLoader implements Closeable
The class methods are inherited from the SecureClassLoader class.
URLConnectionThe class is a parent class of the ones representing a communication between the application and an URL. The class is declared as:
public final class URLConnection extends Object
The class methods are inherited from the Object class.
URLDecoderHTML forms are decoded by this class. The class is declared as:
public final class URLDecoder extends Object
The class methods are inherited from the Object class.
URLEncoderHTML forms are encoded by this class. The class is declared as:
public final class URLEncoder extends Object
The class methods are inherited from the Object class.
URLStreamHandlerThe class is a parent class of all the Stream Protocol Handlers. The class is declared as:
public final class URLStreamHandler extends Object
The class methods are inherited from the Object class.

URL and URI Handling

The java.net package provides classes for working with URLs and URIs. A URL identifies the location of a resource, while a URI identifies a resource or provides a way to represent a resource identifier.

These classes can be used to parse network resource addresses and obtain information such as the protocol, host, port, path, and query.

Example: Using URL

In the following example, the URL class is used to obtain information about a web address.

Java

import java.net.URL;

public class URLExample {
    public static void main(String[] args) throws Exception {
        URL url = new URL("https://www.example.com/index.html");

        System.out.println("Protocol: " + url.getProtocol());
        System.out.println("Host: " + url.getHost());
        System.out.println("Path: " + url.getPath());
    }
}

Output:

Protocol: https
Host: www.example.com
Path: /index.html

Network Communication Using Sockets

The java.net package provides Socket and ServerSocket classes for client-server communication. A server can use ServerSocket to listen for connection requests, while a client can use Socket to establish a connection with the server.

Example: Using Socket

In the following example, a Socket is created to establish a connection with a server running on the local computer.

Java

import java.net.Socket;

public class SocketExample {
    public static void main(String[] args) {
        try {
            Socket socket = new Socket("localhost", 5000);

            System.out.println("Connection established.");

            socket.close();
        } catch (Exception e) {
            System.out.println("Unable to connect to the server.");
        }
    }
}

Output:

Unable to connect to the server.

Working with IP Addresses

The InetAddress class can be used to obtain information about Internet addresses. It can resolve a host name to an IP address and can also provide the host name associated with an address.

Example: Using InetAddress

In the following example, the InetAddress class is used to obtain the host name and IP address of the local computer.

Java

import java.net.InetAddress;

public class InetAddressExample {
    public static void main(String[] args) throws Exception {
        InetAddress address = InetAddress.getLocalHost();

        System.out.println("Host Name: " + address.getHostName());
        System.out.println("IP Address: " + address.getHostAddress());
    }
}

Output:

Host Name: MyComputer
IP Address: 192.168.1.5

The host name and IP address in the output depend on the computer and network configuration.