2

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

lmattcris
  • 49
  • 3
  • 2
    Multiple JAR files on your class path can have a file with that name. By simply using `getResourceAsStream` you effectively pick a random one. – Joachim Sauer Oct 13 '21 at 11:27
  • @JoachimSauer That explains the second test then, I was just clearly wrong with it. What about the first try? – lmattcris Oct 13 '21 at 13:02
  • 2
    Unclear. How do you launch your application? If you launch it from within the IDE then chances are no JAR file is built/involved and instead the classes are loaded directly from the compilation target directory. – Joachim Sauer Oct 13 '21 at 13:08
  • I'm launching both from IDE and directly as `java -jar myapp.jar` -- the application runs as expected – lmattcris Oct 13 '21 at 13:25
  • 1
    Unrelated: why are you going with an empty catch block? Ignoring exceptions is really bad practice, at least print it, so you get a chance to figure what is going on. – GhostCat Oct 15 '21 at 07:14

1 Answers1

0

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.

Queeg
  • 7,748
  • 1
  • 16
  • 42