Reading manifest file from the jar file

M

Manish Hatwalne

I need to read some custom attributes from the manifest.mf file of my jar
file. But somehow I am unable to get the "maniifest.mf" file from my class
in a jar file.
My class is in a package hierarchy in the jar file. The jar file also has
"maniifest.mf" file in its usual location under meta-inf/manifest.mf. How do
I get this file as a resource from my class?

I tried this, but it doesn't work -
this.getClass().getResource("/meta-inf/manifest.mf")

Any help/pointers would be useful.

TIA,
- Manish
 
R

Rogan Dawes

Manish said:
I need to read some custom attributes from the manifest.mf file of my jar
file. But somehow I am unable to get the "maniifest.mf" file from my class
in a jar file.
My class is in a package hierarchy in the jar file. The jar file also has
"maniifest.mf" file in its usual location under meta-inf/manifest.mf. How do
I get this file as a resource from my class?

I tried this, but it doesn't work -
this.getClass().getResource("/meta-inf/manifest.mf")

Any help/pointers would be useful.

TIA,
- Manish

Use the right case for your file name?
 
A

Andrew Thompson

I need to read some custom attributes from the manifest.mf file of my jar
file. But somehow I am unable to get the "maniifest.mf" file from my class
in a jar file.

Why 2 'i's?
My class is in a package hierarchy in the jar file. The jar file also has
"maniifest.mf" file in its usual location under meta-inf/manifest.mf. How do
I get this file as a resource from my class?

I tried this, but it doesn't work -
this.getClass().getResource("/meta-inf/manifest.mf")

now.. you have spelt it twice in inverted
commas as maniifest, and twice as manifest..

Which is factual? (If you are copy/pasting Manish,
it seems to have been from 2 different sources!)
Any help/pointers would be useful.

Rename the .jar to a .zip and look inside it,
or do a list with the jar tool. You may well
find it as /META-INF/MANIFEST.MF or similar,
...and triple check your spelling. ;-)

HTH
 
M

Michael Rauscher

Manish said:
I need to read some custom attributes from the manifest.mf file of my jar
file. But somehow I am unable to get the "maniifest.mf" file from my class
in a jar file.
My class is in a package hierarchy in the jar file. The jar file also has
"maniifest.mf" file in its usual location under meta-inf/manifest.mf. How do
I get this file as a resource from my class?

I tried this, but it doesn't work -
this.getClass().getResource("/meta-inf/manifest.mf")

a) the path name would be /META-INF/MANIFEST.MF
b) in order to get this to work, you have to load each jar with a
separate class-loader. Otherwise you don't know from which jar the
manifest is read (ok, you could work through all paths (jars) of the
class-loader and try to find out in which jar the current class resides
- but, too much work and probably too slow).
c) if you have to load each jar, and thus have to know the filename,
then you could also do a simple

import java.util.jar.*;
//...
JarFile jar = new JarFile("myJar.jar");
Manifest mf = jar.getManifest();
//...

bye
Michael
 
M

Manish Hatwalne

Use the right case for your file name?

Tried that.. No luck!!!

this.getClass().getResource("/meta-inf/Manifest.mf")

Simply returns null. Th problem is with the location. If store same file in
my package hierarchy, such as com/mypack/Manifest.mf it works.
But I want the manifest file in its correct location. I don't even know name
of the jar file at runtime, is here a way I can find it?

- Manish
 
M

Manish Hatwalne

Right. Did all spell checks (keyboard is annoying me)
It is Manifest.mf with single "i"
Here is copy-paste from code -
this.getClass().getResource("/meta-inf/Manifest.mf")

And I have checked it in the jar file, which also contains *SAME* name.

Thanks,
- Manish
 
M

Michael Rauscher

Manish said:
Tried that.. No luck!!!

this.getClass().getResource("/meta-inf/Manifest.mf")

Simply returns null. Th problem is with the location. If store same file in
my package hierarchy, such as com/mypack/Manifest.mf it works.
But I want the manifest file in its correct location. I don't even know name
of the jar file at runtime, is here a way I can find it?

Get the resource URL to your class, e.g. in class com.mypack.MyClass:

URL url = getClass().getResource( "/com/mypack/MyClass.class" );

Now url is something like

jar:file:/path/to/the/jar!/com/mypack/MyClass.class

Now you have to simply strip off the !/... part.

Bye
Michael
 
M

Manish Hatwalne

a) the path name would be /META-INF/MANIFEST.MF

Hey,
You are absolutely right!
But the listing as well as zip showed it as - ("/meta-inf/Manifest.mf")

Thanks it's working! :)
And I hope it would be a portable solution. Isn't jar file has this
specified??

Thanks,
- Manish
 
A

Andrew Thompson

On Thu, 20 May 2004 16:40:54 +0530, Manish Hatwalne wrote:

(Michael Rauscher)
You are absolutely right!

Just curious Manish. Did you read all
the way to the bottom of my post?
 
M

Manish Hatwalne

Andrew Thompson said:
On Thu, 20 May 2004 16:40:54 +0530, Manish Hatwalne wrote:

(Michael Rauscher)

Just curious Manish. Did you read all
the way to the bottom of my post?


I did, but as I said, the listing as well as opening in th eZip showed me -
"/meta-inf/Manifest.mf" :(

Anyway, I have another problem reading from this jar now, but I'll post it
in a separate thread.

Thanks,
- Manish
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top