Win a copy of Pragmatic AI this week in the Artificial Intelligence 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

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,
 
Saloon Keeper
Posts: 9145
173
  • 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: 21424
94
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: 9145
173
  • 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
Marshal
Posts: 38508
653
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!