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
}
Call
What it does
System.inheritedChannel()
The inherited channel, or null.
channel instanceof SocketChannel
One connected client (inetd "nowait" style).
channel instanceof ServerSocketChannel
A 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
Parameter
Description
(none)
inheritedChannel() takes no arguments.
Return Value
Return Value
Description
Channel
The 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:
Method
How it differs
ServerSocketChannel.open()
Opens your own listening channel (the normal way).
Started normally with java SystemInheritedChannelCheck from a terminal or IDE, not by inetd or a similar service.
Inherited channel: none
Normal launch: true
How It Works
A checked exception to handle.inheritedChannel() declares IOException, so the call sits in a try block, though it is almost never thrown.
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".
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));
}
}
}
Terminal output
Started normally with java SystemInheritedChannelKind from a terminal or IDE, not by inetd or a similar service.
This run: none (open your own socket)
Server channel: listening socket (accept clients)
Client channel: client socket (talk to one peer)
How It Works
Checking types with instanceof.Channel is only an interface, so describe tests for the two classes an inetd-style launcher can hand over.
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.
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.
Started normally with java SystemInheritedChannelProvider from a terminal or IDE, not by inetd or a similar service.
Same result: true
Asked again, still null: true
How It Works
Two doors, one room.System.inheritedChannel() simply calls SelectorProvider.provider().inheritedChannel(), the default provider of the java.nio channel classes.
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.
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.