Q:

Compiler encoding system-specific file separator

I have the following java code:
public interface Defaults {
  public static final String DIR_PATH = "a/b/c/";
  File DIR_FILE = new File(DIR_PATH);
}

public class Main {
  public File directory = Defaults.DIR_FILE;
}

This was compiled on a windows machine and deployed to our local nexus repository. It was then executed during a maven build on a linux machine. I end up with main.directory.list() returning null. Using mvnDebug, I see that the path of the File is actually a\b\c! If I use the debugger to change the directory to new File("a/b/c"), then my code works.
Why did the compiler encode system-specific separators? Is there a solution?

A:

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.

