[ BUG ? ] simple code throws on Windows but works fine on *nix
Сергей Цыпанов
sergei.tsypanov at yandex.ru
Tue Jul 2 07:43:02 UTC 2019
Hello,
one of key Java principles is "write once - run everywhere".
It seems to me that this code breaks this rule
------------------------------------
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
public class Main {
public static void main(String[] args) throws IOException {
String path = Main.class
.getClassLoader()
.getResource("tsypanov/example/war-and-peace.json")
.getPath();
List<String> lines = Files.readAllLines(Path.of(path));
assert lines.size() == 1;
}
}
------------------------------------
This code work under *nix (tested on MacOS and Linux Mint)
and fails on Windows 10 with exception:
Exception in thread "main" java.nio.file.InvalidPathException:
Illegal char <:> at index 2: /C:/Users/sergii_tsypanov/IdeaProjects/jdk-bug/out/production/jdk-bug/tsypanov/example/war-and-peace.json
at java.base/sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:182)
at java.base/sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:153)
at java.base/sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77)
at java.base/sun.nio.fs.WindowsPath.parse(WindowsPath.java:92)
at java.base/sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:229)
at java.base/java.nio.file.Path.of(Path.java:147)
at tsypanov.example.Main.main(Main.java:15)
As far as I use a relative path in my Java code
I expect this to have same behaviour on all supported platforms.
This was reproduced on JDK 12.
I've prepared a test project on GitHub to reproduce this in IDEA:
https://github.com/stsypanov/jdk-bug
Regards,
Sergey Tsypanov
More information about the core-libs-dev
mailing list