• 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

    • Floorp 11.29.0 by Razvan Serea Floorp is a cutting-edge web browser that combines the trusted foundation of Mozilla's Firefox with a unique Japanese perspective, offering users an exceptional online experience. This open-source browser prioritizes privacy, customization, and security. Floorp is transparent, with no user tracking or data sharing, and it's completely open source. With a strict no-tracking policy and full transparency, your personal information remains private. As an open-source project, Floorp not only shares its source code but also its build environment, inviting users to contribute and build their unique versions. The regular updates, based on Firefox ESR, ensure that you always have the latest features and security enhancements. Get to the point with Floorp Lightning's minimalism With a keen eye on user preferences, Floorp is gearing up to launch "Floorp Lightning," a streamlined and performance-focused browser, harkening back to the fundamentals of web browsing. This browser has undergone a meticulous transformation, shedding more than 80% of the features that characterized its predecessor. What remains are only the high-demand functionalities within the Floorp ecosystem. The result is a sleek, lean, and swift web browser optimized for maximum efficiency. In the ever-accelerating digital world, "Floorp Lightning" is poised to offer users a refreshingly nimble and responsive browsing experience, set to debut in beta mode this November. Floorp key features: Strong Tracking Protection: Floorp offers robust tracking protection, safeguarding users from malicious tracking and fingerprinting on the web. Flexible Layout: Customize Floorp's layout to your heart's content, including moving the tab bar, hiding the title bar, and more for a personalized browsing experience. Switchable Design: Choose from five distinct designs for the Floorp interface, and even switch between OS-specific designs for a unique look Regular Updates: Based on Firefox ESR, Floorp receives updates every four weeks, ensuring up-to-date security even before Firefox's releases. No User Tracking: Floorp prioritizes user privacy by abstaining from collecting personal information, tracking users, or selling user data, with no affiliations with advertising companies. Completely Open Source: The full source code for Floorp is open to the public, allowing transparency and enabling anyone to explore and build their own version. Dual Sidebar: Floorp features a versatile built-in sidebar for webpanels and browsing tools, making it perfect for multitasking and quick access to bookmarks, history, and websites. Flexible Toolbar & Tab Bar: Customize your browser with Tree Style Tabs, vertical tabs, and bookmark bar modifications, catering to both beginners and experts in customization. User-Centric Web Experience: Floorp prioritizes user privacy and collaboratively blocks harmful trackers. Floorp 11.29.0 changelog: Security fixes Download: Floorp 64-bit | 85.2 MB (Open Source) Links: Floorp Website | Github Website | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • "ethical", what an interesting term. Subjectively objective but objectively subjective. I argue there are very few "ethics" in copyright. Hell, I see very few applications of true ethics in anything. I see a great deal of unethical actions being done under the guise of ethics however. How unethical... I greatly prefer open source LLMs. Copyright ethics are mickey mouse deep.
    • How do you propose anyone govern who can use what online without completely destroying online privacy? Protip: You can't, so you shouldn't. I'm sorry, but I don't think of the children, that's their parents jobs. If their parents fail them, that's on them. Tough luck.
  • Recent Achievements

    • Collaborator
      fernan99 earned a badge
      Collaborator
    • Collaborator
      MikeK13 earned a badge
      Collaborator
    • One Month Later
      Alexander 001 earned a badge
      One Month Later
    • One Month Later
      Antonio Barboza earned a badge
      One Month Later
    • Week One Done
      Antonio Barboza earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      591
    2. 2
      ATLien_0
      225
    3. 3
      Michael Scrip
      167
    4. 4
      Xenon
      139
    5. 5
      +FloatingFatMan
      128
  • Tell a friend

    Love Neowin? Tell a friend!