JDK-8354409 : Parsing a file path on Windows with a trailing space in a name element ("...\ kjdd ggf \...") is rejected
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.nio
  • Affected Version: 11,17,24,25
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • OS: windows_10
  • CPU: x86_64
  • Submitted: 2025-04-10
  • Updated: 2025-04-11
Related Reports
Relates :  
Relates :  
Description
A DESCRIPTION OF THE PROBLEM :
Attempt to construct a Path instance of e.g. 'c:\temp\ kljdsj \file.tmp' - with space at beginnig or end of a path element causes validation failure.
From another side, new File("c:\\temp\\ kljdsj \\file.tmp") allows to operate referenced file as usual and file system has no problems with such file names.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Path.of("c:\\temp").resolve("eruyer \\file.tmp")

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Instance of Path["c:\temp\eruyer \file.tmp"] is successfully constructed
ACTUAL -
Path.of("c:\\temp").resolve("eruyer \\file.tmp") -> InvalidPathException, even if referenced file exists

---------- BEGIN SOURCE ----------
Path.of("c:\\temp").resolve("eruyer \\file.tmp");
---------- END SOURCE ----------
Comments
Per the linked Microsoft page "Naming Files, Paths, and Namespaces": "Do not end a file or directory name with a space or a period. Although the underlying file system may support such names, the Windows shell and user interface does not. However, it is acceptable to specify a period as the first character of a name. For example, ".temp"." A trailing space might be handled by some APIs but it is not by others and it is explicitly discouraged.
11-04-2025

The rejection of directory names ending with a space is deliberate, has been discussed several times in the context of security questions. So I think we should close this issue.
11-04-2025

The observations on Windows 11: JDK 8: Not applicable. JDK 11: Failed, InvalidPathException thrown. JDK 17, 24, and 25ea+6: Failed. This issue is similar to JDK-8190546.
11-04-2025