Search code examples
javaniosubst

How to use Files.getFileStore() for substed drive (on Windows)?


When invoking Files.getFileStore() on a substed drive (on Windows), this results in following error:

The directory is not a subdirectory of the root directory

For example with:

subst P: C:\temp

running:

public static void main(String[] args) throws IOException {
    final Path dir = Paths.get("P:/sub");
    final FileStore fileStore = Files.getFileStore(dir);
    fileStore.isReadOnly();
}

results in:

Exception in thread "main" java.nio.file.FileSystemException: P:\sub: The directory is not a subdirectory of the root directory.

    at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:86)
    at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
    at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
    at sun.nio.fs.WindowsFileStore.create(WindowsFileStore.java:92)
    at sun.nio.fs.WindowsFileSystemProvider.getFileStore(WindowsFileSystemProvider.java:482)
    at java.nio.file.Files.getFileStore(Files.java:1411)
    at utils.FileStoreMain.main(FileStoreMain.java:16)

How to fix this problem and receive the appropriate FileStore for P:?


Solution

  • Have a look at this bug report JDK-8034057 and at a related answer from Alan Bateman.