2

In my Tomcat servlet, I am using a jar library that looks for certain .properties files in System.getProperty("user.dir").

The issue is that when I deploy the servlet on different servers, the "user.dir" is different. I'd like them to be the same. Is there a way to configure this directory so that it is the same for each server?

I also ran catalina.sh version, and the "user.dir" directory doesn't appear in the output (including CATALINA_BASE and CATALINA_HOME), which is strange. This happens for some of the servers. On other servers, the "user.dir" matches CATALINA_BASE.

jh314
  • 27,144
  • 16
  • 62
  • 82
  • 1
    If I were you, I'd contact the library maintainer about this design mistake and get them to fix it. They'd better have read it from the classpath instead which is so much more flexible in Java's context. – BalusC Jan 07 '15 at 18:51

1 Answers1

5

Y̶o̶u̶ ̶c̶o̶u̶l̶d̶ ̶s̶e̶t̶ ̶t̶h̶e̶ ̶p̶r̶o̶p̶e̶r̶t̶y̶ ̶m̶a̶n̶u̶a̶l̶l̶y̶ ̶b̶y̶ ̶m̶o̶d̶i̶f̶y̶i̶n̶g̶ ̶t̶h̶e̶ ̶̶c̶a̶t̶a̶l̶i̶n̶a̶.̶s̶h̶̶ ̶a̶n̶d̶ ̶s̶e̶t̶t̶i̶n̶g̶ ̶i̶t̶ ̶t̶h̶e̶r̶e̶.̶ ̶B̶u̶t̶ ̶t̶h̶i̶s̶ ̶a̶p̶p̶r̶o̶a̶c̶h̶ ̶w̶o̶u̶l̶d̶ ̶b̶e̶ ̶v̶e̶r̶y̶ ̶c̶o̶m̶b̶e̶r̶s̶o̶m̶e̶ ̶s̶i̶n̶c̶e̶ ̶y̶o̶u̶ ̶h̶a̶v̶e̶ ̶t̶o̶ ̶s̶e̶t̶ ̶t̶h̶e̶ ̶v̶a̶l̶u̶e̶ ̶o̶f̶ ̶t̶h̶i̶s̶ ̶f̶o̶l̶d̶e̶r̶ ̶m̶a̶n̶u̶a̶l̶l̶y̶ ̶i̶n̶ ̶e̶v̶e̶r̶y̶ ̶t̶o̶m̶c̶a̶t̶ ̶i̶n̶s̶t̶a̶l̶l̶a̶t̶i̶o̶n̶.̶

Paragraph up here is mistaken. This is because I naively forgot that "user.dir" property is the working folder where Java was executed (example shown here) and you cannot change it not even with -D property when executing the JVM. If you want to change the value of "user.dir", you would have to change the folder where Java executes to start tomcat, and this can be easy or hard, depending on your skills, and I do not recommend this approach as a solution.

A̶n̶o̶t̶h̶e̶r̶ ̶o̶p̶t̶i̶o̶n̶ The best option would be to define a ServletContextListener and set a new property there (and the value as a specific folder), in the contextInitialized method. Then, in any of your servlets, you should recover this property instead.

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • It turns out that for some of my servers, the `user.dir` doesn't match the `CATALINA_BASE` I'd like them to be the same. Would I have to modify `catalina.sh`? – jh314 Jan 07 '15 at 15:28
  • Cool! Would you happen to have any insights as to why `user.dir` is different from `CATALINA_BASE`? – jh314 Jan 07 '15 at 15:35