File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Reading an executable jar's manifest - here's how Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Reading an executable jar Watch "Reading an executable jar New topic

Reading an executable jar's manifest - here's how

Brian Pipa
Ranch Hand

Joined: Sep 29, 2003
Posts: 299
Here's what I was trying to do:
I wanted to read the current version of my Java app from the jar's manifest.
My app is run with a single, excutable jar. It needs to know it's version so that it can automatically check online ofr updates. Previously, I had the version hard-coded in the main class so that when I wanted to update the version, I had to remember to update the two variables (MAJORVERSION and MINIORVERSION). This worked but occassionally I forgot to update the variables.
I do my entire build with Ant and I have Ant needing a version number to create the downloadable zip etc. so I figured I'd just put the version in the jar's manifest automatically with Ant then read it from there in my code.
The problem came in actually trying to read the version from the manifest. It was easy enough if I knew where the jar was since I could use JarFile and Manifest to do it but I didn't know where the jar was on the user's machine.
I found some solutions online that should have worked by calling getResourceAsStream("META_INF/MANIFEST.MF") and getResource("META_INF/MANIFEST.MF") from Class and ClassLoader but those always gave me a manifest from some other jar no matter what I tried.
So, here is the code I came up with to do it. This seems to work for a single excecutable .jar just fine. If anyone sees a problem in this code or knows of a better/cleaner way to do it, let me/us know. Feel free to use this code in any way you want, just leave my name in it as the original author. If your code is packaged in a jar but needs other jars on the classpath to run, modifications to this will be necessary so you can find your jar's manifest. This code works as-is only for single, executable jars.

My Java Freeware:<br />MACCC - <a href="" target="_blank" rel="nofollow"></a><br />Nator - <a href="" target="_blank" rel="nofollow"></a>
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
Very cool
Did you check Joffad when you were working on this? Not that I have either, but I was wondering how your solution differed.

"JavaRanch, where the deer and the Certified play" - David O'Meara
Brian Pipa
Ranch Hand

Joined: Sep 29, 2003
Posts: 299
No, I had never heard of Joffad til you mentioned it. In looking at their info pages, it says "JOFFAD is a generic development framework to facilitate, speed up, and normalize Java projects using JOnAS's J2EE application server."
My project problem/solution has nothing to do with JOnAS or J2EE so I don't think it would have helped
I agree. Here's the link:
subject: Reading an executable jar's manifest - here's how
It's not a secret anymore!