Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a property-file with GlassFish

I'm creating a web service, which run in GlassFish, and I want to have some custom properties. For this I'm using the Properties class. The code I'm using is:

Properties p=new Properties();
File f=new File(System.getProperty("user.dir"), "settings.properties");
p.load(new FileInputStream(f));  

But how do I get the settings.properties-file in my config directory?

I'm not sure about my classpath, since this is managed by NetBeans and GlassFish. I assume my .war-file is added to the classpath, when deploying...

I've added my own solution, but if anyone could come up with a better solution, it would be highly welcome...

like image 424
doekman Avatar asked Feb 10 '09 09:02

doekman


1 Answers

Place your property files in the <glassfish-install-dir>/glassfish/domains/<domain-name>/lib/classes directory and they will be accessible from within your applications via the ResourceBundle class. For example, add a property file named settings.properties to this directory and then access values from the file like this:

ResourceBundle.getBundle("settings").getString("my-property-key");

like image 72
Brent Clay Avatar answered Oct 05 '22 12:10

Brent Clay