I have a java application using the application.properties however once it is packaged to jar file I am no longer able to access values in the application.properties file. How can I load that file on mainly on run time so that i should be able to chenge values and restart the app the use new values.
The application.properties is in the same directory as the java file. Below is the function that is accessing application.properties
public static void main(String[] args)throws Exception{
while (true)classLoader();
}
private static void classLoader() throws Exception {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
Properties properties = new Properties();
try (InputStream resourceStream = loader.getResourceAsStream("application.properties")) {
properties.load(resourceStream);
LocalDateTime now = LocalDateTime.now();
long rebootTime = 0;
String restartTime = properties.getProperty("restart.time");
String restartTime2 = properties.getProperty("restart.time2");
String restartLocalDateTime = now.toLocalDate() +"T"+ restartTime;
String restartLocalDateTime2 = now.toLocalDate() +"T"+ restartTime2;
DateTimeFormatter isoLocalDate = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
LocalDateTime dateTime = LocalDateTime.parse(restartLocalDateTime, isoLocalDate);
LocalDateTime dateTime2 = LocalDateTime.parse(restartLocalDateTime2, isoLocalDate);
if(now.isAfter(dateTime)){
Duration duration = Duration.between(now, dateTime2.plusMinutes(1440));
rebootTime = duration.toMillis();
}else if(now.isAfter(dateTime2)){
Duration duration = Duration.between(now, dateTime.plusMinutes(1440));
rebootTime = duration.toMillis();
}else if(now.isBefore(dateTime)){
Duration duration = Duration.between(now, dateTime.plusMinutes(1440));
rebootTime = duration.toMillis();
}else if(now.isBefore(dateTime2)){
Duration duration = Duration.between(now, dateTime2.plusMinutes(1440));
rebootTime = duration.toMillis();
}
long sleepingMills = Long.parseLong(properties.getProperty("sleeping.time.millis"));
List<String> servicesList = List.of(properties.getProperty("services").split(","));
AbaBundledService abaBundledService = new AbaBundledService();
abaBundledService.doTheProcess(servicesList,sleepingMills);
abaBundledService.verifyTheProcess(servicesList,sleepingMills);
System.out.println("sleeping for "+ rebootTime + " millis");
try {
Thread.sleep(rebootTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
typesafe configis far more flexible than properties