problem with reading config file
Hello,
I have problem with reading config file from the same dir as class is. Situation is:
I have servlet which working with DB, and in confing file (config.pirms) are all info about connection (drivers, users, passw, etc.). Everything work fine if I hardcoded like:
configFileName ="C:\\config.pirms";
I need to avoid such hardcoding, I tryied to use:
configFileName = Pirms.class.getClassLoader().getResourceAsStream ("/prj/config.pirms").toString();
but it isn't work.
My config file is in the same directory as Pirms.class (C:\apache-tomcat-5.5.17\webapps\ROOT\WEB-INF\classes\prj)
Also I tryied BundledResources, it isn't work fo me also.
Can anybody help me to solve this problem? with any examples or other stuff?
Thanks in advance.
Andrew
[874 byte] By [
forumaica] at [2007-10-3 0:14:45]

Where is located your Servlet Class and your "Pirm" class?
Rulasa at 2007-7-14 17:05:11 >

file directory structure:
web.xml (C:\apache-tomcat-5.5.17\webapps\ROOT\WEB-INF\)
Pirms.class (C:\apache-tomcat-5.5.17\webapps\ROOT\WEB-INF\classes\prj)
Pirms.java (C:\apache-tomcat-5.5.17\webapps\ROOT\WEB-INF\classes\prj)
config.pirms (C:\apache-tomcat-5.5.17\webapps\ROOT\WEB-INF\classes\prj)
Sorry for dumb question, where I could to find servlet.class?
This is incorrect :
configFileName = Pirms.class.getClassLoader().getResourceAsStream ("/prj/config.pirms").toString();
getResourceAsStream() returns an InputStream which can then be used to read in the file. Assuming that config.pirms is a properties file you would load a Properties object like this:
Properties pirmsProperties = new Properties();
pirmsProperties.load(
this.getClass().getResourceAsStream ("/prj/config.pirms"));
Another alternative is to use the ServletContext method getResourceAsStream.
This is close in function to the ClassLoader method, but it takes a path relative to the web application root. It is useful for reading properties files in the WEB-INF directory
ie if your config file is in the WEB-INF directory
ServletContext application = getServletConfig().getServletContext();
Properties pirmsProperties = new Properties();
pirmsProperties.load(application.getResourceAsStream("/WEB-INF/config.pirms"));
Cheers,
evnafets
Hello,
I implemented my config file like properties and set to cinfigFileName:
Properties pirmsProperties = new Properties();
pirmsProperties.load(Pirms.class.getClass().getResourceAsStream("/prj/config.pirms"));
String buffer = "";
configFileName = pirmsProperties.toString();
but it isn't work, I am geting an error: "java.lang.NullPointerException"
Can anybody help with this error? What wrong in my code?
Thanks in advance.
Please read envafets's post - use the ServletContext's getResourceAsStream() method.
ServletContext application = getServletConfig().getServletContext();
Properties pirmsProperties = new Properties();
pirmsProperties.load(application.getResourceAsStream("/prj/config.pirms")); //assuming the folder prj is directly under the application root folder
ram.
Note: You can do this only from a Servlet/Jsp. For use in other classes, you'll have to pass the context object along.
Thanks, but I am getting error that "non-static method getServletConfig() cannot be referenced from a static context"
Maybe is it possibility to use <init-param> into web.xml file like:
<init-param>
<param-name>configFile</param-name>
<param-value>/prj/config.pirms</param-value>
</init-param>
If yes can anybody explain how to do that?
I need to have that file for:
FileReader readFile = new FileReader(configFile);
Thanks in advance.
Did you read that note in my last post?You can do this only from a Servlet/Jsp. For use in other classes, you'll have to pass the context object along. So where have you put this code of yours? And where is it being invoked from ?ram.