• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Brian Pipa
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Brian
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic