3

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();
        }
    }
4
  • Unfortunately not working for me. Commented Aug 17, 2021 at 15:19
  • 1
    Why? (must fill space to submit the comment) Commented Aug 17, 2021 at 15:20
  • Needs more details. Where did you put the file in the project? Did the property file actually get placed in the JAR? What code are you using to read the file? Please edit your question to include these details Commented Aug 17, 2021 at 15:21
  • There's nothing really specific about this question vs "reading a file on the classpath", but if you are looking for config files, then typesafe config is far more flexible than properties Commented Aug 17, 2021 at 15:25

1 Answer 1

5

You can check the Java Properties API.

FileReader reader=new FileReader("application.properties");  
Properties p=new Properties();  
p.load(reader);  

This might come handy

Sign up to request clarification or add additional context in comments.

2 Comments

So if I package the java to .jar will the db.properties be included?
@Lewis If the file is in src/main/resources and you use Maven/Gradle default JAR packaging, it should be

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.