General J2EE: Load Properties not using fileinputstream
- Posted by: Vivek kumar
- Posted on: October 25 2004 10:38 EDT
I have to load some Properties file.
Those are packaged in my ear.
Properties take only fileinputstream to load a file. So I have to keep my properties file out side the ear.
Can anybody help me, how to load the properties file from packaged ear
Maybe something like this:
java.net.URL url = this.getClass().getResource("/some-resource.properties");
java.io.FileInputStream pin = new java.io.FileInputStream(url.getFile());
java.util.Properties props = new java.util.Properties();
Artem D. Yegorov
You could also use the java.lang.ClassLoader method getResourceAsStream()...
ClassLoader cl = this.getClass().getClassLoader();
InputStream is = cl.getResourceAsStream(<some location>);
Properties props = new Properties();
However, the root location for the target properties file will be different depending on what type of container you're executing in when the properties are to be loaded...
Executing within a Servlet, the Web Module's classloader targets a root location of <content-root> of the WAR file. So the virtual path within the WAR could be "/WEB-INF".
Executing with an EJB container, the EJB Module's classloader targets that module's virtual root. So you could create a package (e.g. com.serverside.properties) and place your properties file in it, then use...
Property files packaged directly in the EAR (not within a module that the EAR contains) can be loaded using the getBundle() method of java.lang.ResourceBundle. There's a good description of this in the JDK Javadoc.
Hope this helps!