Accessing Properties File
Dear Experts
I am working on java classes using tomcat, I am able to access the properties file located in the "conf" folder under the webapps directory using this servlet code:
File propertiesFile = new File(servletContext.getRealPath("./conf/database.properties"));
but how can i access the same file using java class and not servlet
Any suggestion will be appreciated
Re: Accessing Properties File
Hello there,
My first thought is that you shouldn't use getRealPath as that wont work that well once you package your application in a war file and deploy it.
You need to use one of the getResource methods on the class loader.
Code :
public class IOTest {
public static void main(String[] args) {
final InputStream inputStream = IOTest4.class.getClassLoader().getResourceAsStream("conf/database.properties");
final Properties properties = new Properties();
properties.load(inputStream);
}
}
Now that could would try and load conf/database.properties which would be on the classpath. You should make sure you put these resource files in WEB-INF/classes so your file would be in WEB-INF/classes/conf/database.properties
If you dont put it in classes or WEB-INF folder, you might be exposing the file to the end user as he/she could just browse to http://myserver.domain/mywebapp/conf...ase.properties and see the file.
Hope this makes any sense.
// Json
Re: Accessing Properties File
Dear Json
Many thanks for the reply, it was very helpfull
I need some more help from you in the following:
I am using the ConnectionPoolManager and for that in the getInstance I need to get the properties file either as a File or String
Code :
conMgr = ConnectionPoolManager.getInstance(propsFile);
how can I use your provided solution with the above scenario
Any suggestion will be appreciated
Thanks in advance
Re: Accessing Properties File
I'm not entirely sure what you mean but if you just want to push the properties into the getInstance method could you not just do this?
Code :
public class IOTest {
public static void main(String[] args) {
final InputStream inputStream = IOTest4.class.getClassLoader().getResourceAsStream("conf/database.properties");
final Properties properties = new Properties();
properties.load(inputStream);
conMgr = ConnectionPoolManager.getInstance(properties);
}
}
If not could you tell me what the package name for the ConnectionPoolManager is?
I use iBATIS myself for database stuff. See iBATIS Home for more information.
// Json
Re: Accessing Properties File
Dear Json,
Thanks again for the help
the package I am using is snaq.db.ConnectionPoolManager;
and the method getInstance dose not support java.util.properties
Any Suggestion
Re: Accessing Properties File
How about trying to use the createInstance method.
Code :
ConnectionPoolManager.createInstance(properties);
That should work I believe.
// Json