Is there a way to get the Linux file descriptor for an opened FileChannel?
I need it to call mount.fuse -o fd=... (for implementing FUSE).
As a hacky workaround, I'm doing:
var pid = ProcessHandle.current().pid();
var fd = Files.list(Path.of("/proc/"+pid+"/fd")).count();
var fc = FileChannel.open(path);
System.out.println("file descriptor: " + fd);
Note that two file descriptors appear. One for path and another socket. I'm using the first one. What is the socket for?
You can use reflection to get the file descriptor from a
RandomAccessFile:On Java 11+ you will the jvm option
--add-opens=java.base/java.io=ALL-UNNAMEDat startup. Tested on Java 18.In Java 17 you can use
SharedSecretsinstead of reflection:You will need to add
--add-exports=java.base/jdk.internal.access=ALL-UNNAMEDwhen compiling and running. See for Maven.