• 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

    • Chrome is now faster than ever and Google explains how it did it by Sayan Sen Back in June last year, Google touted some great performance improvements for Chrome and shared a blog post explaining in detail how it managed to achieve them. Today, almost exactly a year later, the search giant is back again with another such post as it continues to make performance gains in its Chromium browser. Interestingly, Google is not the only one to make such claims in recent times. Microsoft also highlighted recently how Edge was getting significantly faster. Both Microsoft and Google have cited the Speedometer 3.0 benchmark to test. We recently measured browsing performance during our T-Force DDR5-7200 RAM review, also using Speedometer 3.0. In its blog post, Google says that the development team made significant improvements to memory management and caching. This includes some redesigning effort of the memory layouts for many internal data structures used in components such as DOM, CSS, layout, and painting. Google says that Blink, the rendering engine in Chromium, now "avoids a lot of useless churn" so as to make better use of the CPU caches. In the areas where memory handling previously relied on garbage collection in Oilpan, like the DOM (document object model), the team has expanded that by shifting from using malloc (memory allocation function) to Oilpan entirely. For those wondering, Olipan is the garbage collector in Blink. Some of the memory management and caching improvements Google made are fundamental to good code optimization. If you recall, recently, a senior Microsoft engineer also pointed out many of these issues in apps that slow Windows down. There are also improvements in handling strings within the renderer; the hashing method was updated to rapidhash, which is said to improve performance. For when rendering tasks become inherently expensive, such as computing CSS styles for various elements, Google adds that caching techniques have been enhanced to achieve higher cache hits and fewer misses.
    • Laptop users, this appears to be single-sided so it should fit even in cases with thin slots.
    • Apple wouldn't be what it is today without China either...
    • I am struggling with the game, after playing it again after so many years.... feels so out of space.
  • Recent Achievements

    • Week One Done
      Uranus_enjoyer earned a badge
      Week One Done
    • Week One Done
      jfam earned a badge
      Week One Done
    • First Post
      survivor303 earned a badge
      First Post
    • Week One Done
      CHUNWEI earned a badge
      Week One Done
    • One Year In
      survivor303 earned a badge
      One Year In
  • Popular Contributors

    1. 1
      +primortal
      428
    2. 2
      +FloatingFatMan
      195
    3. 3
      snowy owl
      191
    4. 4
      ATLien_0
      184
    5. 5
      Xenon
      141
  • Tell a friend

    Love Neowin? Tell a friend!