• 0

[JAVA]Accessing build date/time at runtime


Question

My app is a straightforward Java SE 6 program, built in Eclipse and distributed as a single executable jar (I don't need or use any advanced build tools such as ant).

I want to automate the process of getting accurate version information into the "About" message, because right now I have to remember to open the main class and update a version String every time I rebuild the app.

Ideally I'd like to automatically insert the date/time when the code (or the jar) was built into something I can access at runtime, but any workable approach would do.

Does anyone have any suggestions?

Thanks

James

8 answers to this question

Recommended Posts

  • 0

Use an Ant task to generate a "Version" class at build time that contains the version value as a public static field. You can use that field to get the version information.

<property name="Version" value="1.1.0" />

<target name="build" depends="init">
	...

	<echo file="my/project/Version.java">lic class Version {
	public static final String VERSION = "${Version}";
}
	</echo>
</target>

You could use an Ant <script> task to fill the Version variable if you want something a bit more dynamic.

  • 0

Hi skyfox - thanks for the reply. What I'm looking for is,as you say, "a bit more dynamic", ie something that will automatically make the build date/time available at runtime without the need to edit anything manually for each build. Ps - I've never worked with ant, so maybe the learning curve would be disporportionate to the problem being solved? What do you think?

  • 0

Hi Mike. No, I don't have a script. The app just has a couple of hundred classes, so it's easiest to just let Eclipse keep all the class files up to date (Project > Build Automatically)and then all I have to do is right-click the jar description to get a new jar exported.

  • 0

OK guys, I found a hint that lead me to this

		try {
			File jarFile = new File
			(this.getClass().getProtectionDomain().getCodeSource().getLocation().toURI());
			System.out.println("jar: " + new Date(jarFile.lastModified()));
		} catch (URISyntaxException e1) {
		}

which gives me the last modified date for the jar that the class was loaded from. That's pretty good for what I need...

... unless anyone has a better idea?

Edited by JamesCherrill
  • 0

I found this package's API, which suggest it returns the build date:

http://www.clapper.org/software/java/util/.../BuildInfo.html

More info on it here: http://www.clapper.org/software/java/util/

I took a peek at the source code to see how it got the build date but couldn't find it, it just seemed to store the current date in a property :/

  • 0

^^

I had that same idea James, however I suspect the date time stuff on the jar maybe o/s dependednt (so if you download of the net or switch to unix you may lose your timestamp)...

unless you can read the modified date time off a class inside the jar.... but then again not all class files are always rebuilt when exporting to a jar... or are they?

  • 0

The app is tied in to other Windows services, so I don't need OS independence. With Eclipse automatic build, .java files are re-compiled only when necessary, so the .class file for any given class may be quite old. I haven't tried uploading/downloading it via the net, so maybe that's a problem - I know that just copying it via a shared folder on a server preserves the last modded date.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • F'ing FIIINALLY! Much easier to ask people what their hardware is 🙏 and could also very well just say DDR4 or DDR5 without asking to bloat it too much. I'm also curious to see what it will say in More device info...
    • Sure, that must be why it was revealed at the Sony showcase... the game was announced 5 years ago and the visuals are far from poor, what the hell are you smoking, plus the Switch 2 is pretty much on par with an Xbox Series S. It would be absolutely insane for a dev to build a game "with the Switch 2 in mind" when it's coming to all platforms where the majority of sales will happen.
    • The Irony... China wouldn't be what it is today without Apple 😂
    • Microsoft makes it easier to find PC specs in Windows 11 Settings by Taras Buria Windows 11 has already received several improvements that make it easier to learn about your computer's specifications. Recently, Microsoft released Spec Cards for the System > About section, which provide basic information about the PC's main components, such as processor, memory, storage, graphics card, and video memory. Now, the Settings app is getting a new way to find your device info. Microsoft wants to display basic device information right on the Home page of the Settings app. The latest preview builds from the Dev and Beta Channels introduced a new "Your device info" card for the Settings' Home page. It displays specs like processor name and speed, graphics card and the amount of video memory, storage, and RAM. The card also has a link to the "About" section, where you will find more information about your computer, its Windows edition, product ID, and the recently introduced FAQ section that answers common hardware-related questions. The "Your device info" card joins the existing cards on the Settings app's home page. While the section offers useful information like quick access to Bluetooth devices, Wi-Fi, personalization, and recommended settings, users received it with mixed reactions, as many considered it another way for Microsoft to promote its services and subscriptions like Microsoft 365, OneDrive, and Game Pass (seriously, who thinks about Game Pass when opening Settings?). Now, the Settings' Home page is a bit more useful, as it saves you a few clicks when checking your computer's specs. If you want to test the new "Your device info" card, update your PC to build 26200.5622 or newer (Dev Channel). Just keep in mind that Microsoft is rolling it out gradually, and it requires signing in with a Microsoft Account in the United States. Other changes in build 26200.5622 include a new Settings section for Quick Machine Recovery, widget improvements, more app recommendations in the "Open with" dialog, and more. Check out the full release notes here.
  • Recent Achievements

    • Week One Done
      jbatch earned a badge
      Week One Done
    • First Post
      Yianis earned a badge
      First Post
    • Rookie
      GTRoberts went up a rank
      Rookie
    • First Post
      James courage Tabla earned a badge
      First Post
    • Reacting Well
      James courage Tabla earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      397
    2. 2
      +FloatingFatMan
      178
    3. 3
      snowy owl
      170
    4. 4
      ATLien_0
      167
    5. 5
      Xenon
      134
  • Tell a friend

    Love Neowin? Tell a friend!