月曜日, 8月 02, 2010

JBossProperties

JBossProperties

Read a properties file from a class deployed in Jboss?

   1. Properties properties=new Properties(); 
2. properties.load(Thread.currentThread().getContextClassLoader().getResource("app.properties").openStream());



How do I load a properties file from WEB-INF/classes?
For files directly in the classes directory, our code is like:
        Properties dbProps = new Properties();

//The forward slash "/" in front of in_filename will ensure that
//no package names are prepended to the filename when the Classloader
//search for the file in the classpath
InputStream is = getClass().getResourceAsStream("/"+in_filename);
if(null == is)
{
throw new ConfigException("Can't locate file:" +in_filename);
}
try
{
dbProps.load(is);//this may throw IOException
return dbProps;
}
catch (IOException ioe)
{
System.err.println("Properties loading failed in AppConfig");
throw new ConfigException(ioe,"Can't locate file:" +in_filename);
}

0 件のコメント: