Skip to content

Instantly share code, notes, and snippets.

@AravindaM
Created June 8, 2016 05:47
Show Gist options
  • Save AravindaM/50d05c822bb3191daf5b1e9ca593677a to your computer and use it in GitHub Desktop.
Save AravindaM/50d05c822bb3191daf5b1e9ca593677a to your computer and use it in GitHub Desktop.
Load Config properties outside a JAR file.
public class ConfigReader {
public static Map<String, String> readConfig() {
Map<String, String> config = new HashMap<>(3);
Properties prop = new Properties();
CodeSource codeSource = ConfigReader.class.getProtectionDomain().getCodeSource();
try {
File jarFile = new File(codeSource.getLocation().toURI().getPath());
String jarDir = jarFile.getParentFile().getPath();
System.out.println("Loading config data from :: " + jarDir + File.separator + "config.properties");
InputStream input = new FileInputStream(jarDir + File.separator + "config.properties");
prop.load(input);
config.put("KEY_1", prop.getProperty("KEY_1"));
config.put("KEY_2", prop.getProperty("KEY_2"));
} catch (IOException | URISyntaxException e) {
e.printStackTrace();
}
return config;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment