• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Loading resource in static method

 
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am trying to load the properties file in a static method ( getinstance of a singleton class ) . code goes somthing like

Is there any other method to call getClass().getResourceAsStream(inputfile); in a static method .
thanks,
kripal
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can call getClass() on the Properties object you are loading into.
public static Properties pathProperties = null;

//load in path locations for this environment
static
{
pathProperties = new Properties();
String pathPropertiesFile = "/PATHS.conf";
InputStream paths = pathProperties.getClass().getResourceAsStream(pathPropertiesFile);
if(paths == null)
{
throw new Error("PathConstants::static initializer:: Unable to find configuration file PATHS.conf ");
}
try
{
pathProperties.load(paths);
}
catch(IOException ioe)
{
throw new Error("PathConstants::static initializer:: IOException caught throwing error. " + ioe.getMessage());
}
}

HTH, tom
 
Kripal Singh
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this works thanks tom !!!
 
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But just so you know, Tom's way is different in one subtile way than what you were asking. Calling myClass.getResource("foo") will cause the file to be found by the ClassLoader that loaded MyClass, whereas pathProperties.getClass().getResource() will load the file using the ClassLoader that loaded java.util.Properties - which is always the "system" ClassLoader.
This will work while you have MyClass in the system CLASSPATH, but if you move it to somewhere that uses some other class loading scheme (say to an applet loaded via URL or a servlet or EJB or something like that) then it will quit working because it will look for your properties file to be with the java.util.Properties rather than with your code.
You can do this:
 
please buy this thing and then I get a fat cut of the action:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic