How to use java.util.Properties in an application? Important requirements are:

  • The config should be accessible from a disk for editing, and not to be in .jar
  • The application runs under the crown, so you need to avoid problems with absolute paths.
  • Config must be available in many classes of the program.

About the last point, how can this be done? Use singleton?

 package properties; import java.io.FileInputStream; import java.io.IOException; import java.util.Properties; public class Main { public static void main(String[] args) throws IOException { final String configPath = "src/main/resources/config.properties"; Properties props = new Properties(); props.load(new FileInputStream(configPath)); System.out.println(props.getProperty("mode")); } } 
  • View the owner.aeonbits.org library. On the last point, dependency injection can be used - Maxim
  • @Maxim, in my opinion, is kragokultism, when instead of Properties use the whole library and DI - typemoon
  • pass the path to the configuration as a command line parameter, an environment variable, or look for it in the user's home directory, /etc/вашеприложение or another predefined location - zRrr

2 answers 2

If you need to place the properties file next to jar, you can use this code

 Properties props = new Properties(); // Получаем путь к jar URI uri = MyClass.class.getProtectionDomain().getCodeSource().getLocation().toURI(); // Получаем каталог jar файла String path = new File(uri).getParent(); log.debug(() -> "Load properties from " + path); // Загружаем конфиг try (InputStream file = new FileInputStream(path + "/connection.properties")) { props.load(file); } 

But very often, a regular user will not have rights to edit files in this directory. If this behavior does not suit you, then you can place the properties file in the user's home directory. The path to the directory can be obtained as follows.

 String path = System.getProperty("user.home"); 

    About accessibility everywhere. There is a pretty good implementation of the Properties() extender.

    ExProperty.java code

     public class ExProperties extends Properties { private static final long serialVersionUID = 1L; public void load(final String fileName) throws IOException { load(new File(fileName)); } public void load(final File file) throws IOException { try (InputStream is = new FileInputStream(file)) { load(is); } } public boolean getBoolean(final String name, final boolean defaultValue) { boolean val = defaultValue; final String value; if ((value = super.getProperty(name, null)) != null) val = Boolean.parseBoolean(value); return val; } public String getString(final String name, final String defaultValue) { String val = defaultValue; final String value; if ((value = super.getProperty(name, null)) != null) return value; return val; } public int getInteger(final String name, final int defaultValue) { int val = defaultValue; final String value; if ((value = super.getProperty(name, null)) != null) val = Integer.parseInt(value); return val; } public long getLong(final String name, final long defaultValue) { long val = defaultValue; final String value; if ((value = super.getProperty(name, null)) != null) val = Long.parseLong(value); return val; } public double getDouble(final String name, final double defaultValue) { double val = defaultValue; final String value; if ((value = super.getProperty(name, null)) != null) val = Double.parseDouble(value); return val; } } 

    Prop.java code

     public final class Prop { public static ExProperties _properties; public static int TEST_INTEGER; public static long TEST_LONG; public static final void load() { _properties = initProperties("filename.properties"); TEST_INTEGER = _properties.getInteger("testInteget", -1); TEST_LONG = _properties.getLong("testLong", -1L); } public static final ExProperties getProperties() { return _properties; } public static final ExProperties initProperties(String filename) { final ExProperties result = new ExProperties(); try { result.load(new File(filename)); } catch (Exception e) { System.out.println("An error occured loading '" + filename + "' properties."); } return result; } } 

    Use options:

     public class Test { public static void main(String[] args) { Prop.load(); final int testInt_1 = Prop.TEST_INTEGER; final int testInt_2 = Prop.getProperties().getInteger("testInteger", -1); } } 

    If the configuration file will be located next to the executable file, then I see no reason to specify the paths through the calculation of the location of the executable file. You can simply specify where to look for JAR.

     _properties = initProperties("./Properties.properties"); 
    • It will search relative to the current directory. Check java -jar prog/myprog.jar - Anton Shchyrov