i have 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 compiled on windows machine , deployed our local nexus repository. executed during maven build on linux machine. end main.directory.list() returning null. using mvndebug, see path of file a\b\c! if use debugger change directory new file("a/b/c"), code works.
why did compiler encode system-specific separators? there solution?
unlike else tells you, unrelated windows , file.separator.
string dir_path = "a/b/c/" string, neither java nor compiler make attempt understand mean, same value both on windows , linux.
also note / works file separator both on unix , windows, isn't issue either. when necessary, java convert / \.
file dir_file = new file(dir_path); constant new file() still executed vm @ runtime when class loaded. path not converted windows format while compiler generates byte code.
so problem describe must elsewhere or important part of code example posted changed when simplified question. guess put windows-style path code elsewhere or maybe config , overlooked this.
that said, safe way build paths use file(file, string):
file dir_path = new file( "a", new file( "b", "c" ) ); this build path works, no matter file separator is. if must, can use
file dir_path = "a" + file.separator + "b" + file.separator + "c"; but can fail more complex examples , it's more text read.
Comments
Post a Comment