Over a million developers have joined DZone.

How to Print Text File Content Inside a Jar File

· Java Zone

Microservices! They are everywhere, or at least, the term is. When should you use a microservice architecture? What factors should be considered when making that decision? Do the benefits outweigh the costs? Why is everyone so excited about them, anyway?  Brought to you in partnership with IBM.

Have you ever wonder what's the exact version of your ojdbc6.jar that you have? All the jar files will contain a META-INF/MANIFEST.MF file, and chances are the version will be in it! You may try using $JAVA_HOME/bin/jar -xvf to extract the jar and then view the text file. But afterward you would have to clean up the extracted file so not to liter.

However, if you got Groovy, you can print any text file inside a jar without above mess.

file = new File(args[0])
name = args.size() > 1 ? args[1] : "META-INF/MANIFEST.MF"
jar = new java.util.jar.JarFile(file)
entry = jar.getEntry(name)
istream = jar.getInputStream(entry)
println(istream.text)
istream.close()

You may use this script like this:

$ groovy printjar.groovy /path/to/objdbc6.jar

# Or give an explicit entry name
$ groovy printjar.groovy $JBOSS_HOME/jboss-modules.jar 'META-INF/maven/org.jboss.modules/jboss-modules/pom.properties'

 

 

Discover how the Watson team is further developing SDKs in Java, Node.js, Python, iOS, and Android to access these services and make programming easy. Brought to you in partnership with IBM.

Topics:

Published at DZone with permission of Zemian Deng, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}