Is this possible without knowledge of the name of the main class? In particular I am after the Implementation Version property that I want sent in an info email on application startup. The only ways I know to access this require knowing the package or the name of the jar file whose information I want to access; I do not know how to do it simply by looking for the main class.
-
Look here: [http://stackoverflow.com/questions/1272648/reading-my-own-jars-manifest][1] [1]: http://stackoverflow.com/questions/1272648/reading-my-own-jars-manifest – Lee Meador Dec 28 '12 at 16:20
-
@LeeMeador those answers all assume knowledge of the name of the main class. – djechlin Dec 28 '12 at 16:21
-
It's not clear from your question what context you're in -- if you don't have the jar file name *and* you don't have the package/class name, you'll need to tell us what information we do have to work from. Is this a command-line utility, or something in the context of an Application Server, etc. Are you trying to find manifest attributes of the currently running class? – Gus Dec 28 '12 at 16:36
-
@Gus I'm hoping it's possible to work from the entry point, i.e. the jar of whatever class `java` is passed as the main class. – djechlin Dec 28 '12 at 16:39
-
It might be easier to have your build write a class that each possible main method calls that returns the version number. The main method could then put it into a system property that your email code could use to create the email. – Lee Meador Dec 28 '12 at 22:02
-
If that doesn't work, how does your email sender code get invoked? Is it called from the main method somehow? – Lee Meador Dec 28 '12 at 22:02
2 Answers
The comments from @Lee Meador point at good hints for finding your Manifest. You don't need to know the jar name, you just need to be able to identify your manifest. Suggestion: use any combination of the other IMPLEMENTATION_* attributes to identify the correct manifest. If you're already setting IMPLEMENTATION_VERSION, why not also set IMPLEMENTATION_NAME so you can find it. Once you've got it, you can look through the entries to find the one you want. The Manifest APImakes it even easier:
Attributes mainAtts = mf.getMainAttributes();
if(mainAtts.containsKey(Attributes.Name.MAIN_CLASS)){
String mainClass = mainAtts.getValue(Attributes.Name.MAIN_CLASS);
System.out.println(mainClass);
String mainVer = mainAtts.getValue(Attributes.Name.IMPLEMENTATION_VERSION);
System.out.println(mainVer);
}

- 3,534
- 1
- 30
- 39
So you want to know about the main class but don't want to know what it's name is?
In order to find what you're looking for, you need to:
- Identify the main class (sorry, this will be a requirement)
- Find out where that class was loaded
- If it's a JAR file, load the manifest from that JAR file
Step 1 might be difficult: the thread that launched the main class might have finished -- for instance, many GUI programs will execute a Class.main(String[])
method and then terminate while the AWT thread keeps the process alive. You might not be able to identify the main class at all. If that's the case, you are probably out of luck. You could try the sun.java.command
system property, but that is probably only valid on Sun/Oracle JVMs.
Step 2 is fairly easy: get the Class
's ClassLoader
, then ask for the URL of the .class file itself.
Step 3 is also fairly easy: open the JAR file and look for the META-INF/MANIFEST.MF
file.

- 20,221
- 9
- 60
- 77