如何使用.jar打包在没有springboot的情况下在vanilla java项目中加载`application.properties`

lew*_*ika 3 java

我有一个 java 应用程序,application.properties但是一旦将其打包到jar文件中,我就无法再访问application.properties文件中的值。我如何主要在运行时加载该文件,以便我应该能够更改值并重新启动应用程序以使用新值。

application.properties 与 java 文件位于同一目录中。下面是正在访问的函数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();
        }
    }
Run Code Online (Sandbox Code Playgroud)

raj*_*240 5

您可以检查 Java 属性 API。

FileReader reader=new FileReader("application.properties");  
Properties p=new Properties();  
p.load(reader);  
Run Code Online (Sandbox Code Playgroud)

这可能会派上用场

  • @Lewis如果文件位于 `src/main/resources` 中并且您使用 Maven/Gradle 默认 JAR 打包,则应该是 (2认同)