Runtime version information from manifest in Java

Viewed 250

I have a Java application (compiled with NetBeans 12.5 and OpenJDK 11) where the manifest file is correct, but looks like I'm retrieving the properties from the wrong file(?). Browsing the built jar I have correctly in META-INF/MANIFEST.MF the properties I'm expecting, as in

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.10.8
Created-By: 11.0.11+9-Ubuntu-0ubuntu2.20.04 (Ubuntu)
Bundle-Name: AppName
Bundle-Version: 1.1.3-b000
Bundle-Date: 2021-10-13 10:47:03 CEST
Implementation-Title: AppName
Implementation-Version: 1.1.3-b000
Implementation-URL: http://www.example.com
...

though when my code reads in

String swVersion = getClass().getPackage().getImplementationVersion();

the result is null

Now, with a more complex

  try {
      InputStream is = getClass()
              .getResourceAsStream("/META-INF/MANIFEST.MF");
      if (is != null) {
          Properties p = new Properties();
          p.load(is);
          swRelease = p.getProperty("Implementation-Version");
      }
  } catch ( IOException e )
  {
  }

what I see from debug is that after p.load(is); the properties listed in p are just three

[0] = (ConcurrentHashMap$MapEntry) "Manifest-Version => 1.0"    
[1] = (ConcurrentHashMap$MapEntry) "Created-By => 11.0.5 (Ubuntu)"  
[2] = (ConcurrentHashMap$MapEntry) "Main-Class => org.GNOME.Accessibility.AtkWrapper"

So that's clearly not my MANIFEST.MF ...

I'm a bit confused: any hint?

[Edit] The result wanted is that somewhere, somewhat, I just want to be able to do something like System.out.println(swVersion); and have the output 1.1.3-b000 from the version code as in my manifest

1 Answers

What you are doing matches what I did for Read the jar version for a class.

Therefore I strongly believe you are only seeing an issue when running from within Netbeans, where - as Joachim explained - the application is run from the compiled classes directly - no JAR file involved. I perceive the exact same effect.

Related