0

This is very odd. I try to read the META-INF/MANIFEST.MF from the application itself:

URL url = getClass().getResource("/META-INF/MANIFEST.MF");

But the MANIFEST.MF is another one:

jar:file:/usr/lib/jvm/java-8-oracle/jre/lib/ext/jfxrt.jar!/META-INF/MANIFEST.MF

This worked in the past, but now it locates it in another JAR! Very, very odd.

UPDATED

Tried this as suggested by haraldK but the returned enumeration is empty, so nothing is printed:

try {
    Enumeration<URL> manifests = getClass().getClassLoader().getResources("/META-INF/MANIFEST.MF");

    while(manifests.hasMoreElements()) {
        URL url = manifests.nextElement();
        System.out.println("Es: > " + url);
    }
} catch (Exception e) {
    System.err.println(e.getMessage());
}
Monkiki
  • 109
  • 2
  • 10

2 Answers2

3

Depending on the class loader and the order of the JARs on the class path, this is just normal behavior. As there are multiple instances of /META-INF/MANIFEST.MF, which one you actually get is dependent on the class loader (that loaded the current class, as returned by getClass() in your code).

Instead, you can use ClassLoader.getResouces(String), which will give you an enumeration of all the manifests:

Enumeration<URL> manifests = getClass().getClassLoader().getResources("META-INF/MANIFEST.MF");

From that enumeration, you can look for the right one.

Another option, is using the Package class, which will give you easy access to the information from the manifest for your class/package:

Package myPackage = getClass().getPackage();
myPackage.getImplementationTitle();
myPackage.getImplementationVersion();
myPackage.getImplementationVendor();
Harald K
  • 26,314
  • 7
  • 65
  • 111
  • Tried as suggested but the returned enumeration is empty, so nothing is printed :-\ – Monkiki Feb 14 '18 at 11:44
  • Try skipping the leading slash (ie. `getClassLoader().getResources("META-INF/MANIFEST.MF")`). Might have been a copy/paste bug. – Harald K Feb 14 '18 at 13:14
  • Without the starting slash returns a bunch of URLs, but none of them referring the wanted JAR (some JDK libraries, some Maven dependencies, ...) :/ – Monkiki Feb 15 '18 at 15:36
  • 1
    It was something related to IntelliJ because if I execute the application from command line, the resource appears in the right JAR (second entry because the first one is jfxrt.jar from JDK). Thanks! – Monkiki Feb 16 '18 at 07:37
1

GetClass().getResource behaviour depends on implementation of classloader: https://docs.oracle.com/javase/8/docs/technotes/guides/lang/resources.html Maybe your application got additional classloader?

Similar question is here: Reading my own Jar's Manifest

  • Well, it worked a couple of weeks ago. Maybe the new Java 8 release had some changes related to this behaviour. Thanks! – Monkiki Feb 14 '18 at 11:33