Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Accessing properties file inside a war

I already searched StackOverflow for "properties inside war", but none of the results worked for my case.

I am using Eclipse Galileo and GlassFish v3 to develop a set of web services. I am using a "dynamic web project" with the following structure

Src
-java_code_pkg_1
-java_code_pkg_2
-com.company.config
--configfile.properties WebContent
-META-INF
-WEB-INF
--log4jProperties
--web.xml
--applicationContext.xml
--app-servlet.xml

I want to access the "configfile.properties" inside one of the source files in "java_code_pkg1". I am using the Spring Framework and this file will be instantiated once the application starts on the server.

I have tried the following with no luck

getResourceAsStream("/com.company.config/configfile.properties");
getResourceAsStream("/com/company/config/configfile.properties");
getResourceAsStream("com/company/config/configfile.properties");
getResourceAsStream("/configfile.properties");
getResourceAsStream("configfile.properties");

getResourceBundle(..) didn't work either.

Is it possible to access a file when it's not under the WEB-INF/classes path? if so then how?

like image 902
del.ave Avatar asked Jul 20 '10 15:07

del.ave


People also ask

Where do I put the properties file in war?

These properties files are in the WEB-INF/config directory inside the WAR. You can configure alternative file names and locations by setting the magnolia. initialization.

How do I change application properties in war?

war . In the accounts. war/ WEB-INF/classes folder, open the accounts. properties file, and then edit the following parameters.


3 Answers

Properties props = new Properties();
props.load(this.getClass().getResourceAsStream("/com/company/config/file.properties"));

works when I'm in debug mode. I can see the values in the debugger, but I get a NullPointerException right after executing the "props.load" line and before going into the light below it.

That's a different issue. At least now I know this is the way to access the config file.

Thank you for your help.

like image 128
del.ave Avatar answered Oct 14 '22 16:10

del.ave


If you are in a war, your classpath "current directory" is "WEB-INF/classes". Simply go up two levels.

getResourceAsStream("../../com/company/config/configfile.properties");

It is horrible but it works. At least, it works under tomcat, jboss and geronimo and It works today.

P.S. Your directory structure is not very clear. Perhaps it is:

getResourceAsStream("../../com.company.config/configfile.properties");
like image 28
andcoz Avatar answered Oct 14 '22 16:10

andcoz


Check the location of the properties file in WAR file. If it is in WEB-INF/classes directory under com/company/config directory getResourceAsStream("com/company/config/configfile.properties") should work or getResourceAsStream(" This should work if the config file is not under WEB-INF/classes directoy

Also try using getClass().getClassLoader().getResourceAsStream.

like image 3
Rajendra Avatar answered Oct 14 '22 16:10

Rajendra