I’m looking to embed a piece of code that will print out the time when the current class was last compiled. How can this be implemented in Java?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There is no direct support for this in java, since there is no preprocessor. The closest equivalent is the “Build-Date” attribute in the JAR manifest. Many build systems add this attribute by default, or provide the means to add it.
You can then read the manifest of the JAR at runtime to get the date. The answer to this SO question describes how to read values from the JAR manifest.
The alternative is to use resource filtering to add the date into a properties file, which is then read at runtime. This is quite ad-hoc, non-standard and if you have many jars, with different compilation times, then this quickly becomes difficult to manage, unless you can factor this into a common part of how all the jars are built.