Issue
I am relatively new to Java application programming. I am looking for a way to embed the build time of the application in the About dialog box for the application. It doesn't need to be fancy or in any particular format but does need to change for every build. For a C project I would use the DATE and TIME pre-processor macros.
I tried
private void helpAboutMenuActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
// Trying to launch an About dialog
versionString = versionString.format( "Compiled on %s", compileTime );
System.out.println( "Version Info is: " + versionString );
System.out.println( "this.revision is: " + this.compileTime);
helpAboutDialog.showMessageDialog( helpAboutDialog,
versionString + "\r\n" + this.compileTime, // Message info
"Version Info",
helpAboutDialog.INFORMATION_MESSAGE );
}
where compileTime is defined
public class mainWindow extends javax.swing.JFrame {
/**
* Creates new form mainWindow
*/
public final Date compileTime = new Date();
I then chose "Clean and Build" and ran the .jar file. The time displayed was the time that I ran the .jar file; when I closed the app and ran it several minutes later the time displayed was different. Since I just ran the .jar file my thought is that the time should have been the same.
My build environment is Netbeans 7.4 and the built-in GUI builder (I don't know the proper name for it).
Any suggestions? If necessary I can try to build a complete app that shows the issue (my actual source file is at 800+ lines and growing -- most of which is unrelated to this problem/issue).
My next thought would be to try to find the .jar file in the file system and request the last modified time for the file. Kind of a lot to go through just to get a version number but I know I will get support requests after release and I really will want to know exactly what code was running. Manually modified version numbers usually get forgotten during code fixes/updates in my experience.
Solution
UPDATED: Changed to use the jar file Xxx.class
entry timestamp, instead of the jar file's own timestamp, since a jar file's last modified date may be updated when deployed.
You can use the following code to get the file modified time of the .class
file, from the file system if running directly, or from the jar file entry if packaged.
Class<?> myClass = Test.class; // or getClass() if in non-static method
String classFileName = myClass.getSimpleName() + ".class";
URL classUrl = myClass.getResource(classFileName);
FileTime modifiedTime;
if (classUrl != null && "file".equals(classUrl.getProtocol())) {
modifiedTime = Files.getLastModifiedTime(Paths.get(classUrl.toURI()));
} else if (classUrl != null && "jar".equals(classUrl.getProtocol())) {
JarURLConnection connection = (JarURLConnection) classUrl.openConnection();
modifiedTime = connection.getJarEntry().getLastModifiedTime();
} else {
throw new IllegalStateException("Unable to determine build time for '" + classFileName + "'");
}
System.out.println("File '" + classFileName + "' was modified on " + modifiedTime);
Output when running directly, e.g. from NetBeans
File 'Test.class' was modified on 2020-04-14T01:52:35.2654475Z
Output when running from jar file
File 'Test.class' was modified on 2020-04-14T01:52:34Z
Answered By - Andreas