javawindowspathtostring

String of java.nio.file.Path


Can someone explain what I'm doing wrong and/or missing here. I have java.nio.file.Path objects that work fine in my Java application. The only issue is when I try to print them out in logs, and the issue is only present on Windows. When I stringify a Path object (or File object), I get some path like build/somefile.txt on Linux, Unix, Mac, but on Windows it prints as buildsomefile.txt. What I would expect to see is build\\somefile.txt, but instead, the slashes are ommitted on Windows.

This is the simple example code that I've executed on Mac, Ubuntu, and Windows with Java 11 and Java 17:

import java.nio.file.Paths;

System.out.println(Paths.get("build", "somefile.txt"));
System.out.println(Paths.get("build", "somefile.txt").toFile());
System.out.println(Paths.get("build", "somefile.txt").toFile().getPath());

Same behavior if I normalize the path, convert to an absolute path, etc. From the docs of Path, it looks to me like toString() should use File.separator here, which is the behavior I do see on non-Windows systems. The docs also do specify that the Windows separatorChar is \\. Can anyone help me understand what I'm missing here? Thanks!

EDIT:

These all also output empty strings:

import java.nio.file.File;
System.out.println(File.separator);
System.out.println(File.separatorChar);

It's worth noting I'm seeing this in tests, if that is relevent.


Solution

  • java is emitting build\someFile.txt as you'd expect. To be specific, java emits a byte sequence to standard out which then goes on a trip. That goes into the OS, the OS sends it to whatever's hooked up to your java proces's standard out which at some point presumably takes those bytes, converts it back to a string, and then does something with that. Hopefully in this wild adventure eventually somewhere something turns it into a little picture of some characters and these, eventually, are rendered onto your screen, and from there, into your eyeballs.

    Something in that wild adventure is applying backslash escaping. Terminals don't do it, so something else that you did not mention in your question is doing that. Java certainly doesn't do it. Something else is. If you just write this extremely basic:

    class Test {
      public static void main(String[] args) throws Exception {
        Path x = Path.of("foo/bar.txt");
        System.out.println(x);
      }
    }
    

    and run that on a stock windows in a stock-standard windows cmd.exe terminal that prints foo\bar.txt as you'd expect.

    Check the chain of apps that are in between the java process and your eyeballs. One of em is messing this up. If you need some help with this process, edit the question and explain in excruciating detail (because you probably don't know what's relevant, this mojibake / terminal stuff can be tricky like that) each and every app or thing that could possibly be in between. Are you running this within an IDE? Which one, and how. Are you running this on windows subsystem for linux? Mention that, how you're doing it, which commands you're typing in, and so on.