• Post Reply Bookmark Topic Watch Topic
  • New Topic

Adding a properties file to Jar or War  RSS feed

 
ravinderSingh singh
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Is it possible to add a properties file, which stores various configuration information, in a JAR or WAR archive. The aim being that the application can read the configuration information when it is deployed/run.

Thanks.
 
vidhyasagar reddy
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no problem u can do it
but when ur trying to acess the properties file
use the relative path to acess it
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can store any file you like in a Jar and access it via ClassLoader.getResourceAsStream(). My application, with an embedded Web server, has all the config files, images etc. in a Jar.
 
ravinderSingh singh
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could anyone give me an example (i.e. outline code of either solution above) since I'm new to using Property files and WAR/JAR files.

Thank you
 
Chris Montgomery
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sure. This example is gathering jdbc data from a properties file. Although, storing database users and passswords inside the jar may be a security concern. anyway...

Lets say you have a package called com.my.test.properties.
and in that package, you have a file called jdbc.propreties

in my jdbc.properties file, I have a line that looks like this:
JDBC_DRIVER=com.mysql.jdbc.Driver

From any other class in your project, use the following code to access the data stored in your properties file.



Also, if you find you are loading your ResourceBundle many times in different methods, you may want to consider a singleton approach, so you don�t have to load from the properties file each time you want to get its information.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!