Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load `application.properties` in vanilla java project without springboot using .jar packaging

Tags:

java

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();
        }
    }
like image 576
lewis machilika Avatar asked Jun 11 '26 22:06

lewis machilika


1 Answers

You can check the Java Properties API.

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

This might come handy

like image 153
raj240 Avatar answered Jun 13 '26 12:06

raj240



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!