0

I need to be able to read the contents of a file (say MANIFEST) within a jar file. Currently I do this by extracting the contents of the file and then listing it

${JAVA_HOME}/bin/jar xvf SOME_WAR_FILE.war META-INF/MANIFEST.MF
cat META-INF/MANIFEST.MF

Is it possible to do this using a one liner, without extracting the contents ?

souser
  • 5,868
  • 5
  • 35
  • 50
  • Is this file deployed or not? If it's deployed, it's contents should already be extracted and can be read directly (I know this is possible w/ JBoss and WebLogic) – Kon Apr 10 '14 at 23:18
  • I need this to perform some validation before I startup my app server, so I cannot wait until I startup the app server. – souser Apr 10 '14 at 23:23
  • And just to clarify, your requirements are to do this without extracting the contents physically? Or that you don't care how it gets done, but you want a one line solution? – Kon Apr 10 '14 at 23:28
  • without extracting the contents – souser Apr 10 '14 at 23:30
  • I'm not on my dev machine so I can't try it, but how about the solution suggested here: http://stackoverflow.com/questions/14778980/how-to-read-content-of-the-zipped-file-without-extracting-in-java – Kon Apr 10 '14 at 23:32
  • Problem is that I have over 1000 servers. If I write a java program, I will need to copy it over to all of them. – souser Apr 10 '14 at 23:39
  • 1
    First, 1,000 servers is a bit ridiculous. Second, what kind of solution DO you want then, if not a programmatic one? – Kon Apr 10 '14 at 23:46
  • @Kevin - my solution below uses a standard tool that should already be on your machine(s), it displays the file contents without extracting it, yet Kon seems to think it's wrong and deserves a downvote. Can you tell me what about my answer does not satisfy your requirements? Kon's comment on my answer was... less than informative. – Stephen P Apr 10 '14 at 23:54
  • @StephenP Not sure how you didn't understand my comment, but that's not for me to explain. I've retracted my downvote after your explanation, but I doubt this will satisfy OP. – Kon Apr 11 '14 at 00:01
  • possible duplicate of [How to read MANIFEST.MF file from JAR using Bash](http://stackoverflow.com/questions/7066063/how-to-read-manifest-mf-file-from-jar-using-bash) – Ken Y-N Apr 11 '14 at 00:05

3 Answers3

4

Is this what you are looking for?

unzip -qc SOME_WAR_FILE.war META-INF/MANIFEST.MF

See the previous answer here.

Community
  • 1
  • 1
Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
  • This is exactly what I was looking for. I liked the -q (quiet) option as it displays only the contents of the file. – souser Apr 11 '14 at 16:08
1

What about this?

unzip -p your.jar META-INF/MANIFEST.MF | cat

the -p option says:

extract files to pipe, no messages

That | cat isn't really necessary. It'll print to standard out without it.

Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356
0

A jar or war file is just a standard zip compressed archive, so you can use standard unzip with the -c option to direct it to stdout

$ file your.jar
your.jar: Zip archive data, at least v1.0 to extract
$ unzip -c your.jar META-INF/MANIFEST.MF
file contents
file contents
Stephen P
  • 14,422
  • 2
  • 43
  • 67
  • Yes. This displays the file without extracting it. In what way is this wrong? – Stephen P Apr 10 '14 at 23:49
  • I don't have a Linux machine, but wont' this extract the files specified to the temp directory while reading? – Kon Apr 10 '14 at 23:52
  • No, it does not. I just re-ran that command (I of course tested it before posting an answer) while watching my /tmp directory. If something was extracted there it was too fast to see, and it *certainly* did not extract the entire .jar -- if anything it would have been only the one file requested. – Stephen P Apr 10 '14 at 23:56