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");
/etc/вашеприложениеor another predefined location - zRrr