Created
June 8, 2016 05:47
-
-
Save AravindaM/50d05c822bb3191daf5b1e9ca593677a to your computer and use it in GitHub Desktop.
Load Config properties outside a JAR file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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