Access NFSv3 exports directly from Java without mounting them in the operating system. The library provides file and stream APIs for reading, writing, listing, and managing remote files, plus direct access to NFSv3 protocol operations.
This maintained fork of the EMC NFS client is available under the Apache License 2.0. It requires Java 17 or newer and an NFS server exposing NFSv3 and MOUNT v3 over TCP.
For NFSv4, Kerberos authentication, and support under a commercial license, visit nfs-client.dev.
Add version 1.2.0 from Maven Central.
<dependency>
<groupId>io.github.denysobukh</groupId>
<artifactId>nfs-client</artifactId>
<version>1.2.0</version>
</dependency>implementation 'io.github.denysobukh:nfs-client:1.2.0'The library uses SLF4J 2.0. Supply an SLF4J 2.0-compatible logging provider in your application. When upgrading from an older client version, update the logging provider too: SLF4J 1.7 bindings are ignored. See Logging and diagnostics for configuration and migration details.
This example reads an existing /hello.txt file from an export. Replace the
server, export path, and UNIX user/group IDs with values appropriate for your
server.
import com.emc.ecs.nfsclient.network.NetMgr;
import com.emc.ecs.nfsclient.nfs.io.Nfs3File;
import com.emc.ecs.nfsclient.nfs.io.NfsFileInputStream;
import com.emc.ecs.nfsclient.nfs.nfs3.Nfs3;
import com.emc.ecs.nfsclient.rpc.CredentialUnix;
import java.io.IOException;
public class ReadNfsFile {
public static void main(String[] args) throws IOException {
try {
var credential = new CredentialUnix(1000, 1000, null);
var client = new Nfs3("nfs.example.com", "/export", credential, 3);
var file = new Nfs3File(client, "/hello.txt");
try (var input = new NfsFileInputStream(file)) {
input.transferTo(System.out);
}
} finally {
NetMgr.getInstance().shutdown();
}
}
}Nfs3File paths are relative to the export: / refers to the export root, and
/hello.txt refers to /export/hello.txt on this server. The final constructor
argument, 3, sets the maximum number of NFS call attempts.
CredentialUnix sends AUTH_SYS user and group IDs. The server's export rules
and file permissions determine access; choose IDs that the server permits.
Reuse the client for operations on the same export. Network resources are shared
across clients in the application. Close streams after use and call
NetMgr.getInstance().shutdown() once, when all NFS work in the application is
finished, rather than after each file operation.
NfsFileInputStream and NfsFileOutputStream implement Java's standard stream
APIs. With the client from the quick start, write a file as follows:
import com.emc.ecs.nfsclient.nfs.io.Nfs3File;
import com.emc.ecs.nfsclient.nfs.io.NfsFileOutputStream;
import java.nio.charset.StandardCharsets;
var file = new Nfs3File(client, "/greeting.txt");
try (var output = new NfsFileOutputStream(file)) {
output.write("Hello from Java!\n".getBytes(StandardCharsets.UTF_8));
output.flush();
}The output stream creates the file if it does not exist and writes from offset
zero by default. It does not truncate an existing file, so writing shorter
content can leave trailing bytes. Call flush() explicitly when write or commit
failures must reach your code: close() logs and suppresses flush failures.
Nfs3File also supports metadata, directory creation, renaming, deletion, and
symbolic links. See the API documentation
for method details and stream constructors with explicit offsets and buffer sizes.
import java.util.Comparator;
var directory = new Nfs3File(client, "/");
var files = directory.listFiles();
files.sort(Comparator.comparing(Nfs3File::getName));
for (var file : files) {
System.out.println(file.getName());
}list() and listFiles(), including filtered overloads, preserve the server's
entry order and omit . and ... NFSv3 does not guarantee alphabetical order;
sort the complete returned list when you need predictable output, as above.
Filtering preserves the relative order of accepted entries.
A listing can span multiple server responses. Continuation cookies do not provide a snapshot of a directory being modified, so concurrent changes can affect the results.
By default, the client discovers NFS and MOUNT ports through the server's portmapper on TCP port 111. For fixed ports, firewalls, or port forwarding, set the destination ports explicitly:
var credential = new CredentialUnix(1000, 1000, null);
var client = new Nfs3("nfs.example.com", "/export", credential, 3,
12049, 20048);Each port accepts 1 through 65535; 0 enables discovery independently for
that service. For example, 12049, 0 fixes the NFS port and discovers the MOUNT
port. Specifying both ports avoids portmapper. Pass the hostname or IP address
separately from the ports.
If you already have the export's root file handle, use the full constructor to skip MOUNT:
var client = new Nfs3("nfs.example.com", "/export", rootFileHandle,
credential, 3, 12049, 0);With a supplied root handle and an explicit NFS port, construction requires no RPC calls. Hostname resolution still applies. Explicit ports remain in effect if the client refreshes the root handle.
client.getPort() returns the active NFS destination port.
client.getMountPort() returns the configured or last discovered MOUNT port,
or 0 if unknown. Neither getter performs discovery.
- Mount access denied: check the exported path, the server's client allowlist, UID/GID permissions, and any privileged source-port requirement. Destination port settings do not change the client's source-port privileges.
- Service unavailable or connection failure: confirm that NFSv3 and MOUNT v3 are exposed over TCP and reachable through the firewall. When using discovery, portmapper must also be reachable on TCP port 111.
- Missing logs after upgrading: install an SLF4J 2.0-compatible provider and remove old SLF4J 1.7 bindings.
For request, endpoint, retry, and protocol-status diagnostics, enable DEBUG
logging for com.emc.ecs.nfsclient. See Logging and diagnostics.
Licensed under the Apache License 2.0. Based on the original EMC/Dell NFS client, with thanks to its maintainers and contributors.