java properties files

I mentioned last night that I was attempting to use property files. So let’s say you have a class in a jar file that reads a property file and the property file is in the jar. Is there any way to load the property file without knowing exactly on the file system where the property file lives? Should I be using a ResourceBundle instead?

2 thoughts on “java properties files”

  1. The classloader has methods for this purpose. So, from a class that is in the same JAR as the property file (or really, if you’re not using multiple classloaders, any class) you can do this:

    Properties props = new Properties();

    InputStream propFileStream = this.getClass().getClassLoader().getResourceAsStream(“com/cephas/myprops.properties”);

    props.load(propFileStream);

Leave a Reply

Your email address will not be published. Required fields are marked *