[ BUG ? ] simple code throws on Windows but works fine on *nix
Alan Bateman
Alan.Bateman at oracle.com
Tue Jul 2 08:16:43 UTC 2019
On 02/07/2019 08:43, Сергей Цыпанов wrote:
> 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();
Resource name -> URL -> URL path component. A URL path component is not
a file path. For file URLs then it encodes a file path (at least file
paths that are not located on the network). There's a warning in the URL
javadoc on this but maybe it's time to deprecate URL::getPath (in time
we need to deprecate all of URL constructors and several methods but
that is a topic for another day). For the example, change getPath to
toURI so you get a URI rather than a String and it should work.
-Alan
More information about the core-libs-dev
mailing list