Java System inheritedChannel() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 5+

Definition

In Java, System.inheritedChannel() returns the channel inherited from the program that started the JVM, or null if there is none. Here a channel is an open network socket from java.nio.channels.

This only matters when a Unix service manager such as inetd starts the program with a socket already attached. Programs you start yourself from a terminal or IDE normally get null.

A static method of the java.lang.System class, added in Java 5 (JDK 1.5). Call it on the class name, as in System.inheritedChannel(...). No import is needed, because classes in java.lang are always visible.

Syntax

Ask for the channel and check what you got:

Java
Channel channel = System.inheritedChannel();  // throws IOException
if (channel == null) {
    // normal launch: open your own sockets
} else if (channel instanceof ServerSocketChannel) {
    // started by a service manager with a listening socket
}
CallWhat it does
System.inheritedChannel()The inherited channel, or null.
channel instanceof SocketChannelOne connected client (inetd "nowait" style).
channel instanceof ServerSocketChannelA listening socket to accept clients from.

Added in Java 5. The JDK checks for an inherited channel once and remembers the answer, so later calls return the same object (or null again).

Library declarations
Java
// java.lang.System
public static Channel inheritedChannel() throws IOException

Parameters

ParameterDescription
(none)inheritedChannel() takes no arguments.

Return Value

Return ValueDescription
ChannelThe inherited channel, often a SocketChannel or ServerSocketChannel, or null if none was inherited.

Compare with Similar Methods

Related ways to get or reach an I/O channel:

MethodHow it differs
ServerSocketChannel.open()Opens your own listening channel (the normal way).
SocketChannel.open()Opens your own client channel.
console()Another "what was I started with?" check, for the terminal.

Java Examples

Example 1 — Check for an Inherited Channel

Most programs are started by a person, not a service manager, so the expected answer is "none". The example also handles the declared IOException.

Java
import java.io.IOException;
import java.nio.channels.Channel;

public class SystemInheritedChannelCheck {
    public static void main(String[] args) {
        try {
            Channel channel = System.inheritedChannel();
            System.out.println("Inherited channel: " + (channel == null ? "none" : "found"));
            System.out.println("Normal launch: " + (channel == null));
        } catch (IOException e) {
            System.out.println("Could not check: IOException");
        }
    }
}

How It Works

  1. A checked exception to handle. inheritedChannel() declares IOException, so the call sits in a try block, though it is almost never thrown.
  2. Nothing was handed over. Started from a terminal or IDE, the JVM has no socket from its parent process, so the method returns null and the ternary prints "none".
  3. A launch-mode flag. channel == null doubles as "this was a normal launch", which a program can use to decide whether to open its own server socket.

Example 2 — Decide What to Do Based on the Channel Type

A service-style program reacts differently to a listening socket, a single client, or nothing. Two self-opened channels show the other branches without any network traffic.

Java
import java.io.IOException;
import java.nio.channels.Channel;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

public class SystemInheritedChannelKind {
    static String describe(Channel channel) {
        if (channel == null) {
            return "none (open your own socket)";
        }
        if (channel instanceof ServerSocketChannel) {
            return "listening socket (accept clients)";
        }
        if (channel instanceof SocketChannel) {
            return "client socket (talk to one peer)";
        }
        return "other channel";
    }

    public static void main(String[] args) throws IOException {
        System.out.println("This run: " + describe(System.inheritedChannel()));
        try (ServerSocketChannel server = ServerSocketChannel.open();
             SocketChannel client = SocketChannel.open()) {
            System.out.println("Server channel: " + describe(server));
            System.out.println("Client channel: " + describe(client));
        }
    }
}

How It Works

  1. Checking types with instanceof. Channel is only an interface, so describe tests for the two classes an inetd-style launcher can hand over.
  2. This run inherits nothing. A normal start returns null, so the first line takes the "none" branch, which is what a regular server program would see.
  3. Exercising the other branches. ServerSocketChannel.open() and SocketChannel.open() create unconnected channels without network traffic; try-with-resources closes both.

Example 3 — Compare with SelectorProvider.inheritedChannel()

System.inheritedChannel() is a shortcut. The real work is done by the default SelectorProvider, and both report the same remembered result.

Java
import java.io.IOException;
import java.nio.channels.Channel;
import java.nio.channels.spi.SelectorProvider;

public class SystemInheritedChannelProvider {
    public static void main(String[] args) throws IOException {
        Channel fromSystem = System.inheritedChannel();
        Channel fromProvider = SelectorProvider.provider().inheritedChannel();
        System.out.println("Same result: " + (fromSystem == fromProvider));
        System.out.println("Asked again, still null: " + (fromSystem == null && System.inheritedChannel() == null));
    }
}

How It Works

  1. Two doors, one room. System.inheritedChannel() simply calls SelectorProvider.provider().inheritedChannel(), the default provider of the java.nio channel classes.
  2. Identical answers. Both calls return the same reference. In a normal launch that is null twice; under inetd it would be the same channel object twice, so == is true in both cases.
  3. The answer is remembered. The JDK looks for an inherited channel only once per JVM, so asking a third time still gives null; the combined check is true.

Frequently Asked Questions

null. A channel is only inherited when a service manager such as inetd starts the JVM with a socket attached.
A Unix service manager that listens on network ports and starts a program for each connection, with the socket attached.
Wrapping an inherited socket in a channel object is I/O that can fail, so the checked exception is declared, though it is rarely thrown.
Expect null: the JDK supports inherited channels only on Unix-like systems.
No. A normal server opens its own ServerSocketChannel; inheritedChannel() is for programs started by a service manager.

Next: lineSeparator()

The line break of the current platform: "\n" on Linux and macOS, "\r\n" on Windows.

lineSeparator() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful