Unlike what everyone else tells you, this is unrelated to Windows and File.separator
.
String DIR_PATH = "a/b/c/"
is just a String, neither Java nor the compiler make any attempt to understand what it could mean, so you get the same value both on Windows and Linux.
Also note that /
works as a file separator both on Unix and Windows, so this isn't an issue either. When necessary, Java will convert /
to \
.
File DIR_FILE = new File(DIR_PATH);
is a constant but new File()
is still executed by the VM at runtime when the class is loaded. So the path is not converted to Windows format while the compiler generates byte code.
So the problem you describe must be elsewhere or an important part of the code example you posted was changed when you simplified it for the question. My guess is that someone put a Windows-style path into the code elsewhere or maybe the config and you overlooked this.
That said, a safe way to build paths is to use File(File, String)
:
File DIR_PATH = new File( "a", new File( "b", "c" ) );
This will always build a path that works, no matter what the file separator is. If you must, you can use
File DIR_PATH = "a" + File.separator + "b" + File.separator + "c";
but that can fail for more complex examples and it's more text to read.