0

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); 
    }

}
user207421
  • 305,947
  • 44
  • 307
  • 483
user1672382
  • 87
  • 1
  • 10
  • In Java a non static field can't be used in a static method, but the the opposite is possible: https://www.tutorialspoint.com/can-we-make-static-reference-to-non-static-fields-in-java – Harry Coder Apr 15 '22 at 04:07

1 Answers1

0

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.

Kaneda
  • 722
  • 5
  • 16