Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

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

 
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,
 
Bartender
Posts: 9494
184
  • 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.
 
Sheriff
Posts: 21465
97
Chrome Eclipse IDE Java Spring Ubuntu VI Editor 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
Bartender
Posts: 9494
184
  • 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().
 
author & internet detective
Posts: 38909
684
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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!