'"Cannot make a static reference to the non-static field" error when setting a variable to System.setProperty [duplicate]

I'm trying to assign a String variable to a system property fetched from the application.properties in main method. But it gives me

Cannot make a static reference to the non-static field LOG_FILE

This is my code snippet, what is the mistake I'm making here?

@SpringBootApplication
public class MqMessageHandlerApplication {

    @Value("${logging.home.file}")
    String LOG_FILE;    
    
    public static void main(String[] args) {
        //System.setProperty("LOG_DIR", "D:\\mq-message-handler-1.0\\logs\\" );
        System.setProperty("LOG_DIR", LOG_FILE );
        SpringApplication.run(MqMessageHandlerApplication.class, args); 
    }

}


Solution 1:[1]

The main method is a static method, it is called without a object of your class MqMessageHandlerApplication being instantiated, this being said, you can't call the LOG_FILE, it will only be accessible when you instantiate a object of MqMessageHandlerApplication. Try to fetch this file inside the main body, not using a field injection, then it will work.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kaneda