SampleApp.war
|
|-------- myApp.properties
|
|-------- WEB-INF
|
|---- classes
|
|----- org
|------ myApp
|------- MyPropertiesReader.class
Let's assume that you want to read the property named "abc" present in the properties file:
----------------
myApp.properties:
----------------
abc=some value
xyz=some other value
Let's consider that the class org.myApp.MyPropertiesReader present in your application wants to read the property. Here's the code for the same:
package org.myapp;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* Simple class meant to read a properties file
*
* @author Jaikiran Pai
*
*/
public class MyPropertiesReader {
/**
* Default Constructor
*
*/
public MyPropertiesReader() {
}
/**
* Some Method
*
* @throws IOException
*
*/
public void doSomeOperation() throws IOException {
// Get the inputStream
InputStream inputStream = this.getClass().getClassLoader()
.getResourceAsStream("myApp.properties");
Properties properties = new Properties();
System.out.println("InputStream is: " + inputStream);
// load the inputStream using the Properties
properties.load(inputStream);
// get the value of the property
String propValue = properties.getProperty("abc");
System.out.println("Property value is: " + propValue);
}
}
Pretty straight-forward. Now suppose the properties file is not at the root of the application, but inside a folder (let's name it config) in the web application, something like:
SampleApp.war
|
|-------- config
| |------- myApp.properties
|
|
|-------- WEB-INF
|
|---- classes
|
|----- org
|------ myApp
|------- MyPropertiesReader.class
There will just be one line change in the above code:
public void doSomeOperation() throws IOException {
//Get the inputStream-->This time we have specified the folder name too.
InputStream inputStream = this.getClass().getClassLoader()
.getResourceAsStream("config/myApp.properties");
Properties properties = new Properties();
System.out.println("InputStream is: " + inputStream);
//load the inputStream using the Properties
properties.load(inputStream);
//get the value of the property
String propValue = properties.getProperty("abc");
System.out.println("Property value is: " + propValue);
}
That's all that is required to get it working.
22 comments:
How is it that the getResourceAsStream knows where to look to find the properties file? Why does it know to look starting from the top of the WAR?
The getResourceAsStream looks for a resource in the classloaders. As you can see in the code, we first get access to the classloader which has loaded your application (.war) and then invoke the getResourceAsStream method which starts searching this classloader for the resource.
I have tried to put this answer in simple terms. The javadocs have a very good explanation for the same:
getResourceAsStream
getResource
Refer to following article on apache site -
http://tomcat.apache.org/tomcat-4.1-doc/class-loader-howto.html
It talks about class path and class loading within war files.
hello , ı am Faruk
I am dog tired while tring to do this problem,
thanks about your solution
best regards
Just what I was looking for. Spent the better part of my lunch break trying to figure this out :-(
Thanks, man!
I am new for java development and I find what i was searching for. Thanks a lot to My Wiki
I am using netbeans 6 and deploying my app to sun webserver 7, due to some reason it is not working, any suggestions pleassssss
Ok, that's great but how do I read the properties file from java when I'd like to put the properties file into the Apllication Server properties folder
eg. AppServer/properties
For me, using eclipse and tomcat6.0.13, it was necessary to put my connection.xml file into /WEB-INF/classes directory. Then I used the first way to create InputStream:
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("connection.xml");
Whats difference between ResourceBundle class which loads .properties file and the above InputStream approach ? Which one is better ?
I think, when we use ResourceBundle , we can't load a file from directory which is outside the CLASSPATH , Am I right ? Pl reply !
Thanks,
In the above example you mentioned are you trying read a file which is not part of the war file but it is from a application module.
I am here talking about ear deployment
Then how to read a resource
If I want the web service to ask Tomcat on which port and url it is running, which resource should I use?
This info was very helpful to me - thanks!
Thank you, I had to change an application that someone else had written and had never used a properties file before (or maybe I just forgot). After banging my head against the wall decided to search and found your info. thanks very much. Ann
You can even use it without explicitly calling the getClassLoader()
InputStream is = getClass().getResourceAsStream("/init.properties");
Make sure you take note of the "/" at the begining. The "/init.properties" needs to be in the WEB-INF/classes folder
Hi jaikiran My Name is nagaraju. i am also a java/j2ee developer. i would like to create one blog like u, can u tell me how did u create u r blog. plz.............I am so impressed for u r blog.
Hi Nagaraju,
blogspot.com allows you to create blogs of your own. There are other services like wordpress.com too which allow you to create blogs for free.
On the top right corner of my blogspot, you will find a "Create Blog" link. Just click on that link and follow the instructions.
How about is the method is static method that returns property value ? In that you can not instantiate some thing like this.class.
For example in my case I have utility method,
public static String getPropertyValue(String name){
Properties properties = new Properties();
try {
//This is not working properties.load(new FileInputStream("/webapps/DayParser/enviornment.properties"));
return properties.getProperty(name);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
I found this worked only when I put the properties file in the "WEB-INF\classes" folder.
It did not work when it was in the WEB-INF directory.
I am using NetBeans, JDK 6 and a web service application on Glassfish.
I am using JDK 1.5 and Tomcat 5.25
I call from a jsp
.
.
import="org.myApp.MyPropertiesReader"
.
.
.
.
MyPropertiesReader mp = new MyPropertiesReader();
mp.doSomeOperation();
This is the console with the exception
INFO: Server startup in 906 ms
InputStream is: null
03/12/2009 13:27:12 org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: Servlet.service() para servlet jsp lanzó excepción
java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Properties.java:365)
at java.util.Properties.load(Properties.java:293)
at org.myApp.MyPropertiesReader.doSomeOperation(MyPropertiesReader.java:39)
at org.apache.jsp.index_jsp._jspService(index_jsp.java:55)
========================
and I also from a servlet
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
MyPropertiesReader mp = new MyPropertiesReader();
mp.doSomeOperation();
}
This is the console with the exception
INFO: Server startup in 907 ms
InputStream is: null
03/12/2009 13:25:21 org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: Servlet.service() para servlet Prueba lanzó excepción
java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Properties.java:365)
at java.util.Properties.load(Properties.java:293)
at org.myApp.MyPropertiesReader.doSomeOperation(MyPropertiesReader.java:39)
at org.servlet.Prueba.doGet(Prueba.java:45)
======
Please
What I am doing wrong?
Post a Comment