Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Print Text File Content Inside a Jar File

DZone's Guide to

How to Print Text File Content Inside a Jar File

· Java Zone
Free Resource

Build vs Buy a Data Quality Solution: Which is Best for You? Gain insights on a hybrid approach. Download white paper now!

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'

 

 

Build vs Buy a Data Quality Solution: Which is Best for You? Maintaining high quality data is essential for operational efficiency, meaningful analytics and good long-term customer relationships. But, when dealing with multiple sources of data, data quality becomes complex, so you need to know when you should build a custom data quality tools effort over canned solutions. Download our whitepaper for more insights into a hybrid approach.

Topics:

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

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}