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.