This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Brian Pipa
Ranch Hand
Posts: 299
  • 0
  • Mark post as helpful
  • send pies
  • 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.

 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic