-1

When I was working with XCode and iOS, there was a simple way to check the application's current version by reading the plist.

Is there a similar way to do this in Java?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Luca
  • 1,704
  • 3
  • 29
  • 42
  • Which app? Something you wrote? What do you mean with "it came from Xcode"? – Matteo Sep 04 '12 at 09:06
  • 1
    Its so simple there are many ways of storing and retrieving it. ;) Also a typical Java app consists of many library versions so there are multiple versions which may be of interest. What version are you trying to get? – Peter Lawrey Sep 04 '12 at 09:08
  • Is it a Java application or an iOS application? – Matteo Sep 04 '12 at 09:19
  • 1
    @Matteo he is talking about a Java application, the OP says that in XCode it was easy to know that app version but he doesn't know how to do it in Java. – Alonso Dominguez Sep 04 '12 at 09:21
  • @AlonsoDominguez Ok, now I get it. But the question remains: he wants to get the version number using Java but he never told if the application is also in Java. He also speaks of iOS. It could be that he wants to read the version of an iOS app with Java. – Matteo Sep 04 '12 at 09:23
  • @AurA I would not remove the Java tag as he is explicitly mentions Java in the question. – Matteo Sep 04 '12 at 09:25
  • @Matteo touché! some clarification around that would be great – Alonso Dominguez Sep 04 '12 at 09:25

3 Answers3

4

XCode stores that version value in a resource file that is distributed with your application. In Java the equivalent would be your Manifest file, which is packed inside your JAR/WAR/EAR archive.

A Manifest file is just a metadata text file named MANIFEST.MF that stores some standard key/value pairs which are recognized by many tools and that is packaged inside a special folder named META-INF inside your java archive.

To get the Manifest file for your own JAR this question would give you some clues. Once you have your own Manifest instance then use either one of the next options to get that version value.

This way to get the Specification Version:

Manifest mf = .... // get the manifest file
String specVersion = mf.getAttribute("Specification-Version");

This way to get the Implementation Version:

Manifest mf = .... // get the manifest file
String specVersion = mf.getAttribute("Implementation-Version");

More info regarding the JAR manifests can be found here.

EDIT:

If you are getting null values for any of those properties that means that they haven't been configured in your MANIGEST.MF file. That's easy to check: unzip your JAR file (JAR files are just ZIP files with a different extension name) and go the META-INF folder to find the MANIFEST.MF file, since it's a text file you can print its contents to the console, if there is a Specification-Version or Implementation-Version attribute defined there and you are still getting null values then you might be loading a manifest file from a different JAR.

FOR THE RECORD:

To get that attributes in your Manifest file you would need to configure your build tool to do so. Maven would do it automatically (you can customize it though), with Ant you will need to use a specific Ant Task, with Eclipse you will need go through its docs (same with any other IDE).

Community
  • 1
  • 1
Alonso Dominguez
  • 7,750
  • 1
  • 27
  • 37
  • when I execute the getAttribute method in both case I recieve back a null string. – Luca Sep 04 '12 at 10:29
  • actually the two properties are missing in the manifest file. I try o understand how to configure NetBeans to include that information in the manifest file. Thanks! – Luca Sep 04 '12 at 11:28
  • Of course! I accept the answer now because the problem is in the manifest file and not in your code! thanks! – Luca Sep 04 '12 at 19:48
0

As Alonso says, in Java, your code isn't automatically assigned a build version by the compiler. Java leaves that up to the build tool that your compiler is run by, e.g. ant or maven. If your app isn't using the manifest file, which is often the case, but using instead a version number suffix, e.g. my_app_1.2.3.jar then you could do this to get the jar name:

Class.getProtectionDomain().getCodeSource().getLocation()
Adam
  • 5,215
  • 5
  • 51
  • 90
  • Note that would not work in an applet or JWS app. The path returned would always point back to the server, even if all resources are cached locally. – Andrew Thompson Sep 04 '12 at 09:47
0

If it has a GUI and the main purpose1 is 'update' use Java Web Start to deploy it.

For displaying the version to the user I would store the version number in the manifest of each Jar of the app., and show the Implementation-Versions in a JTable at run-time.

  1. As an aside, to get better answers, put aside what you are trying to 'do' and instead name the 'feature' you are trying to achieve. The latter can be expressed as the application feature you would like to offer the user. It might be something like 'Can be expanded with plug-ins', 'Has free auto-upgrade for 24 months', 'Whiter, brighter, more suds'..
Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433