Java Servlet absolute paths outside webfolder [ Windows & Linux ]

1k views Asked by At

Disclaimer : At the moment, due to lack of a Linux dev/test server, I am currently unable to test this myself. Hence me asking the question here. I will have a Linux box eventually but am currently confined to using Windows.

I am designing a webapp that will run in Tomcat 7 on Windows & Linux.

Supposing on both systems the WAR is deployed in the following locations (respectively)

/opt/Tomcat/webapps/MyApp

C:/opt/Tomcat/webapps/MyApp

The Webapp has a Servlet which needs to process files from the OS file system in the following locations

/work/logs/<logfiles>

C:/work/logs/<logfiles>

On Windows I can specify C:/work/logs and the Servlet knows to pick up this absolute path and it works fine. I suspect because the C: at the start. I know this because I have tested this.

My real question is...

On Linux, in the absence of a drive letter, if I ask it to look in /work/logs will it try to look at a relative path :

/opt/Tomcat/webapps/MyApp/work/logs

or will it look in the file system (/work/logs) as I'd like it to?

I'm asking this now because it will ultimately affect the overall design.

2

There are 2 answers

2
AlexR On BEST ANSWER

Leading slash in Linux means "absolute path", so you can be sure that if you use path like /work/logs/<something> it will understand it as an absolute path.

BTW if you use the same path in Windows it will work and use current disk, i.e. if tomcat's working directory is on C: it will use C: drive, however if tomcat is running on D: this drive will be used.

1
Joeblade On

I can recommend using CATALINA_HOME environment variable to find a folder in which you place your config. For my projects I have the following structure:

$CATALINA_HOME/appconfig/   <-- config
$CATALINA_HOME/webapps/somewar.war     <-- your webapps  

Inside appconfig I will place a somewar.properties which functions as the configuration for that server. (I also place log4j.properties, and any special certificates / other things that are specific to the server instance you run on, but I try to keep it to a minimum) As there will always be a CATALINA_HOME set for your project it is reusable.

inside the somewar.properties I would then list

work.path=/work/logs

Using /work/logs will work on both linux and windows as @AlexR mentions (in his much more to the point answer) but you can get into trouble as on linux you would need root access to be allowed to create the /work folder. It depends on who is managing the server and how strict they are. I prefer the configuration solution so if you end up working on a machine that won't let you use a certain location, you can switch. Ofcourse you do need to be allowed to write inside CATALINA_HOME ;)