Files.isHidden C:\\ changed between JDK12 and JDK13 on windows
Asked Answered
A

2

6

Files.isHidden(Path.of("c:\\")) returns true on Windows 10, JDK 13

but returns false on JDK 12 same machine.

Anyone know why this is?

Anemochore answered 30/9, 2019 at 13:32 Comment(2)
Which vendor did you get your JDK from? What is the exact version of both of them? Do you have any download links maybe? Can you still reproduce it?Contrabassoon
JDK 13 downloaded from here: oracle.com/technetwork/java/javase/downloads/index.htmlAnemochore
C
6

It was a bug that was fixed with JDK 13.

On Microsoft Windows, the java.nio.file.Files.isHidden method has historically ignored the DOS "hidden" attribute on directories. This has been fixed in this release so that isHidden now returns true when invoked to test a directory that has this attribute set.

From the release notes

Cake answered 30/9, 2019 at 13:37 Comment(6)
This is true, but I doubt C:\ has its hidden attribute set.Acrimonious
@Acrimonious this explains the difference, not what weird configuration OP may have on their machine :)Myxomatosis
I tried to read the attributes of C:\ with attrib c:\ on my machine, and it said File C:\ not found. (And yes, C:\ is my HD, so it exists.)Dalpe
Also, it appears that only C:\ is returning hidden, other drives do not.Anemochore
@Cake , isHidden appeared to be working properly for me on JDK 12 and previous releases.Anemochore
@VGR, i've tried this on two separate Windows 10 machines, same problem. Having to put ugly hack in code to avoid this issue. Windows doesn't seem to allow hidden flag on drives, so I think this is a bug in the JDK.Anemochore
L
6

As already mentioned, the difference in behavior is due to a bug being fixed: JDK-8215467. The description of the bug explains that, before the fix, the result of Files#isHidden(Path) was inconsistent with other core software on Windows (e.g. File Explorer, PowerShell, CMD, etc.). The inconsistency was that directories in Windows certainly can be hidden but Java (or at least NIO2) thought otherwise.

In the comments to the issue it was pointed out the result was also inconsistent with java.io.File#isHidden(). In fact, if you use:

File file = new File("C:\\");
System.out.println(file.isHidden());

You'll see true printed out, even in Java 12 and older (at least I do on my Windows 10 Home machine).

The fact C:\ is being reported as hidden appears to be correct for me. If I check the attributes of C:\ in PowerShell it shows the directory as hidden.

PS C:\> $root = Get-Item "C:\"
PS C:\> $root.Attributes
Hidden, System, Directory
Liquidation answered 30/9, 2019 at 14:18 Comment(2)
this looks correct. Just funky windows I guess. if you select "Don't show hidden files" it still shows the C: in explorer. I guess Explorer just treats this drive differently.Anemochore
It does appear that way, though I don't know why. I've been searching for an answer but have been unsuccessful. Note that my other drive, D, is also apparently hidden. However, if I insert a USB drive it is not hidden. I thought maybe the difference was that C and D had the "System" attribute (and this answer seemed to indicate the System attribute had similarities to the Hidden attribute) while my USB drive did not, but other system folders/files are also not hidden.Liquidation

© 2022 - 2024 — McMap. All rights reserved.