1

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.

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Ah, I hadn't considered the notion that the IDE and terminal could be hiding this from me, great observation! It's a fairly simple application, so my guess is it's getting swallowed in the Gradle and/or JUnit flow somewhere ... I'll keep poking around and update if I find exactly what is swallowing it fro my display. Thanks!
I don't usually develop on Windows, I only recently noticed this in log output from my GitHub Actions for Windows builds, which is why I got curious about it. When I hopped on a Windows VM and ran make tests from the Command Prompt, I saw the same issue. From your suggestion though, I downloaded IntelliJ on the Windows VM and dropped a breakpoint. Sure enough, the `` is there, so all is well. I'm very curious why GitHub Actions / Gradle and Windows Command Prompt are swallowing this character, but since it's only aesthetic, I'm not worrying about it for now. Thanks for helping me confirm!
Lots of java programmers and even non-java programmers are habitually just using / as path separator in their scripts and the like. In java it's a long tradition because all path specs in code can have that (Path.of("dir/subdir/file.txt") works on windows, as does new File("C:/Program Files/whatever") and always has). That puts even less pressure on e.g. github to make backslash work properly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.