tips on avoiding hard coded paths and make a java app platform independent

HI all,

I would like to gather some tips on how to avoid hard coded file paths, which are plenty in the application I work on and which I inherited from previous programmers.

Also, when deploying to the production server, my app goes from a Windows platform to a Linux machine.

I am curious to know what tricks and tools other programmers find useful in similar contexts.

I am sure many of us would benefit from the discussion.

Thank you !

[480 byte] By [GraziaRa] at [2007-11-27 10:31:27]
# 1

Actually, Java does this for you; you can use unix-style paths and they will still work, even on Windoze. To wit:

public static void main(String[] args) {

File f = new File("/");

System.out.println(f.getAbsolutePath());

}

Yields: C:\

public static void main(String[] args) {

File f = new File("/Program Files");

System.out.println(f.getAbsolutePath());

}

Yields: C:\Program Files

jGardnera at 2007-7-28 18:09:43 > top of Java-index,Java Essentials,Java Programming...
# 2

Resources as in getResourceAsStream.

java.util.prefs.Preferences

System properties like "user.home".

malcolmmca at 2007-7-28 18:09:43 > top of Java-index,Java Essentials,Java Programming...
# 3

Thanks very much.

I also know that since file and directory separators differ between platforms one can use

System.getProperty("file.separator")

or use the static variable java.io.File.separator in order to determine the file separator for the current platform.

GraziaRa at 2007-7-28 18:09:43 > top of Java-index,Java Essentials,Java Programming...
# 4

Or... ya know... you could just use the linux path style and let Java do it for you.

jGardnera at 2007-7-28 18:09:43 > top of Java-index,Java Essentials,Java Programming...