pat5star
asked on
how to call properties file from utility class
Hi all,
I'm trying to load a properties file in a utility class so that I can get it from either servlets or other utility classes. Below is my code I've been trying to do it with, which keeps throwing a NPE error. I've tried placing sql.properties in /WEB-INF and /WEB-INF/classes to no avail.
What I"m trying to achieve is a class that only loads the properties file once and then retains it in memory for quick access from other classes. Any insight/suggestions to my approach is certainly welcome :-)
Thanks in advance,
-Pat
import java.io.InputStream;
import java.util.Properties;
import org.apache.log4j.Logger;
public class MyProperties {
static Logger logger = Logger.getLogger(MyPropert ies.class. getName()) ;
private static Properties props = null;
private static InputStream in = null;
static {
try {
in = getClass().getResourceAsSt ream("/WEB -INF/sql.p roperties" );
props.load(in);
logger.info("sql.propertie s loaded");
} catch (Exception e) {
logger.error("Unable to load sql.properties, the error is: " + e);
} finally {
try {
in.close();
} catch (Exception ignored) {}
}
}
public static Properties getProps() {
return props;
}
}
I'm trying to load a properties file in a utility class so that I can get it from either servlets or other utility classes. Below is my code I've been trying to do it with, which keeps throwing a NPE error. I've tried placing sql.properties in /WEB-INF and /WEB-INF/classes to no avail.
What I"m trying to achieve is a class that only loads the properties file once and then retains it in memory for quick access from other classes. Any insight/suggestions to my approach is certainly welcome :-)
Thanks in advance,
-Pat
import java.io.InputStream;
import java.util.Properties;
import org.apache.log4j.Logger;
public class MyProperties {
static Logger logger = Logger.getLogger(MyPropert
private static Properties props = null;
private static InputStream in = null;
static {
try {
in = getClass().getResourceAsSt
props.load(in);
logger.info("sql.propertie
} catch (Exception e) {
logger.error("Unable to load sql.properties, the error is: " + e);
} finally {
try {
in.close();
} catch (Exception ignored) {}
}
}
public static Properties getProps() {
return props;
}
}
with your properties file in:
/WEB-INF/classes
/WEB-INF/classes
You can save the reference in the application context
In fact you could probably put it in a servlet class and load it at startup so it's ready immediately for access
Is props initialized?
I would rather suggest that put properties file in a classpath, and access it with
String fileName = obj.getClass().getResource ("/sql.pro perties"). getFile();
File file = new File(fileName);
This is package independent so evenif you move your sql.properties file from
some.pkg. folder to some.other.pkg , you dont need to change the code for that. just change the env classpath setting and will work.
String fileName = obj.getClass().getResource
File file = new File(fileName);
This is package independent so evenif you move your sql.properties file from
some.pkg. folder to some.other.pkg , you dont need to change the code for that. just change the env classpath setting and will work.
> I would rather suggest that put properties file in a classpath, and access it with
Thats what Pat is attempting as I understand it.
And what the suggestion I posted above will achieve.
Thats what Pat is attempting as I understand it.
And what the suggestion I posted above will achieve.
yep, objects you were right partially..in the sense that code you gave was perfect. but
with your properties file in:
/WEB-INF/classes
this line was not required..only requirement is that file should be in classpath..
somehow i am not comfortable with putting propertis file with .class files...it should be outside of ear file.
with your properties file in:
/WEB-INF/classes
this line was not required..only requirement is that file should be in classpath..
somehow i am not comfortable with putting propertis file with .class files...it should be outside of ear file.
>..only requirement is that file should be in classpath..
this line is ambiguous...what I meant was...the directory where .properties file resides should be in the classpath..not the file itself.
this line is ambiguous...what I meant was...the directory where .properties file resides should be in the classpath..not the file itself.
> i am not comfortable with putting propertis file with .class files.
but you want them in the classpath :) whats the difference?
you could put them in:
/WEB-INF/classes/props/sql .propertie s
And then use:
in = getClass().getResourceAsSt ream("/pro ps/sql.pro perties");
For this example another suggestion would be to specify the properties in web.xml, and not worry about an external properties file at all.
but you want them in the classpath :) whats the difference?
you could put them in:
/WEB-INF/classes/props/sql
And then use:
in = getClass().getResourceAsSt
For this example another suggestion would be to specify the properties in web.xml, and not worry about an external properties file at all.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
There is one other thing though. How are you getting your code to compile??? As the method call getClass() is not static, you will need an instance of the class in order to use it.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Very true.... I guess he didn't post the exact code.
ASKER
Problem solved, here is the working code now:
import java.io.InputStream;
import java.util.Properties;
import org.apache.log4j.Logger;
public class MyProperties {
static Logger logger = Logger.getLogger(MyPropert ies.class. getName()) ;
private static Properties props = null;
private static InputStream in = null;
static {
try {
in = MyProperties.class.getReso urceAsStre am("/sql.p roperties" );
props = new Properties();
props.load(in);
logger.info("sql.propertie s loaded");
} catch (Exception e) {
logger.error("Unable to load sql.properties, the error is: " + e);
} finally {
try {
in.close();
} catch (Exception ignored) {}
}
}
public static Properties getProps() {
return props;
}
}
I really appreciate everyones input on this. I would like to split the points up because it was a combination of Objects, mayankeagle, and grim_toaster's comments that helped me get this working. Can I even split them up 3 ways or is there a better solution? What would make everyone happy? Because I'm happy that it's working now :-)
CEHJ: I just wanted to mention that I appreciate your suggestion as I know that would work as well. I was going to revert to that if I couldn't get this working but I really prefer this approach as I don't have to bother with load-on-startup in the web.xml and because I plan to add to this class later as well. Thanks :-)
-Pat
import java.io.InputStream;
import java.util.Properties;
import org.apache.log4j.Logger;
public class MyProperties {
static Logger logger = Logger.getLogger(MyPropert
private static Properties props = null;
private static InputStream in = null;
static {
try {
in = MyProperties.class.getReso
props = new Properties();
props.load(in);
logger.info("sql.propertie
} catch (Exception e) {
logger.error("Unable to load sql.properties, the error is: " + e);
} finally {
try {
in.close();
} catch (Exception ignored) {}
}
}
public static Properties getProps() {
return props;
}
}
I really appreciate everyones input on this. I would like to split the points up because it was a combination of Objects, mayankeagle, and grim_toaster's comments that helped me get this working. Can I even split them up 3 ways or is there a better solution? What would make everyone happy? Because I'm happy that it's working now :-)
CEHJ: I just wanted to mention that I appreciate your suggestion as I know that would work as well. I was going to revert to that if I couldn't get this working but I really prefer this approach as I don't have to bother with load-on-startup in the web.xml and because I plan to add to this class later as well. Thanks :-)
-Pat
You have allocated enough points to this question to split between all the people who have answered on this page. Just click on the Split link and then you can split points between everyone, giving them as much as you want, out of 500.
Mayank.
Mayank.
ASKER
Done. Thanks all.
-Pat
-Pat
try:
in = getClass().getResourceAsSt