Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

properties file outside of the WAR that works both on linux and Windows  RSS feed

 
a pazwant
Greenhorn
Posts: 11
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi There,

I have a war file to be deployed in Glassfish installed in Linux and Windows OS. I am trying to externalize the config.properties file so it can be changed without re-deploying war file. I can make it by referencing 'absolute path' such as C:\Temp\config.properties; but of course it does not work in Linux due to a different file system.

I was wondering how it could be done to work in both linux and Windows environments ? Also I would like to hear any best practices or approaches related to deployments on different OSs.

Thanks for your help,
 
Stephan van Hulst
Saloon Keeper
Posts: 7808
142
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The properties file should always be in a fixed location relative to your WAR. Use the java.nio.file.Path class to refer to it. You can use Paths.get() to get such a path. Use one argument per path segment, don't use slashes or backslashes. The paths that are returned are file system agnostic.
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:The properties file should always be in a fixed location relative to your WAR.

Shouldn't you use ServletContext.getRealPath? That's the official way to get a path to something inside your WAR. You can then use Path to go up as far as you like. That can still be troublesome because the container might deploy the files to some temp folder.

WebLogic has an environment variable (DOMAIN_HOME) that indicates where its domain home is, and that's what we use at work to determine a proper base location. JBoss has some system variables that can be used in a similar way (e.g. jboss.server.config.dir). These work on both Windows and Linux. Does GlassFish also have something like that?
 
Stephan van Hulst
Saloon Keeper
Posts: 7808
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suppose you're right. I would probably go for something provided by the framework one is using though. For instance, in Spring you'd use ApplicationContext.getResource().
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37241
519
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not a best practice, but a best hack - call System.getProperty("my.external.config.file") from the code that reads the property file. Then set that property on your linux and Windows servers to the right location.

The reason it isn't a best practice is relative paths are better. But not always possible...
 
a pazwant
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks you all.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!