Explain Codes LogoExplain Codes Logo

Reading my own Jar's Manifest

java
manifest-engineering
class-loader
package-metadata
Anton ShumikhinbyAnton Shumikhin·Jan 25, 2025
TLDR

Get your JAR's Manifest attribute values swifter than lightning with this little corner-cut gem:

Manifest manifest = new Manifest(getClass().getResourceAsStream("/META-INF/MANIFEST.MF")); String attributeValue = manifest.getMainAttributes().getValue("Attribute-Key");

Just swap "Attribute-Key" with the actual attribute name from the manifest file. Make sure to run this code from a class located in the JAR.

Checking class origin

Before you ride off into the sunset with your manifest, you have to check if your class really came from a JAR file. A simple URL check is all it takes:

URL jarUrl = YourClass.class.getResource(YourClass.class.getSimpleName() + ".class"); if (jarUrl != null && jarUrl.toString().startsWith("jar:")) { // Class comes from a JAR, and just like finding a lost sock, it's time to celebrate! }

Direct access with JarURLConnection

Fancy tools for the modern-age coder: use a JarURLConnection to access the manifest directly:

JarURLConnection jarConnection = (JarURLConnection)jarUrl.openConnection(); Manifest manifest = jarConnection.getManifest();

This trick works only if you're feeding it a URL with the "jar" protocol (just like feeds to Gremlins, remember the rules).

Deploying jcabi for simplicity

If cleaning up after Java API sounds like chores, the jcabi-manifests library might be your housemaid:

String attrValue = Manifests.read("My-Attribute");

Enjoy the cleaner way to fetch manifest attribute values, less code, fewer hassles.

Digging info from Java package

Java's own API also hides some manifest golden nuggets in the Package metadata:

Package pkg = YourClass.class.getPackage(); String implVersion = pkg.getImplementationVersion(); String implTitle = pkg.getImplementationTitle(); // Just like magic, more package-related information at your fingertips.

This feature gets especially handy when you want the version info, without the manual labor of handling the manifest file.

Taming non-standard class loaders

Non-standard class loaders like OSGi or JBoss VFS can sometimes act like feral cats, but that doesn't mean you can't tame them:

// Let's pretend it's a VFS URL URL vfsUrl = ...; // Imagine we got the VFS URL (magic, right?) if (vfsUrl.toString().startsWith("vfs:/")) { // More magic happens - Like pulling a rabbit out of a hat might involve a few odd-looking operations! }

When sailing the uncharted waters of an OSGi environment, like Apache Felix, remember to check the compass to ensure your solution adheres to OSGi package exporting mechanisms.

Plowing through restricted fields

Working in a restricted environment like applets or Java Web Start feels like plowing a rocky field. The security manager might restrict access to the manifest, but don't lose heart, solutions are often just a forum post away.

Handling edge cases

Mystery of the wrong manifest

A word of caution: if you have many JARs hiding in the classpath, make sure you're not playing hide and seek with the wrong manifest. Like a good detective, verify your clues (URLs).

The OSGi-specific attribute

For those who chose the path of OSGi, the Export-Package attribute in the manifest is the map for package exporting and bundle interactions. Handle it with care!

Restricted areas and attributes

Getting manifest access in high-security zones like applets or Web Start applications could be as tricky as mastering a Rubik's cube blindfolded. It may require some creative thinking or alternate methods for exposing necessary packages.