Is there any way i can read the contents of a jar file. like i want to read the manifest file in order to find the creator of the jar file and version. Is there any way to a
Achieve the attributes in this simple way
public static String getMainClasFromJarFile(String jarFilePath) throws Exception{
// Path example: "C:\\Users\\GIGABYTE\\.m2\\repository\\domolin\\DeviceTest\\1.0-SNAPSHOT\\DeviceTest-1.0-SNAPSHOT.jar";
JarInputStream jarStream = new JarInputStream(new FileInputStream(jarFilePath));
Manifest mf = jarStream.getManifest();
Attributes attributes = mf.getMainAttributes();
// Manifest-Version: 1.0
// Built-By: GIGABYTE
// Created-By: Apache Maven 3.0.5
// Build-Jdk: 1.8.0_144
// Main-Class: domolin.devicetest.DeviceTest
String mainClass = attributes.getValue("Main-Class");
//String mainClass = attributes.getValue("Created-By");
// Output: domolin.devicetest.DeviceTest
return mainClass;
}
Next code should help:
JarInputStream jarStream = new JarInputStream(stream);
Manifest mf = jarStream.getManifest();
Exception handling is left for you :)
You can use a utility class Manifests from jcabi-manifests:
final String value = Manifests.read("My-Version");
The class will find all MANIFEST.MF
files available in classpath and read the attribute you're looking for from one of them. Also, read this: http://www.yegor256.com/2014/07/03/how-to-read-manifest-mf.html
Keep it simple. A JAR
is also a ZIP
so any ZIP
code can be used to read the MAINFEST.MF
:
public static String readManifest(String sourceJARFile) throws IOException
{
ZipFile zipFile = new ZipFile(sourceJARFile);
Enumeration entries = zipFile.entries();
while (entries.hasMoreElements())
{
ZipEntry zipEntry = (ZipEntry) entries.nextElement();
if (zipEntry.getName().equals("META-INF/MANIFEST.MF"))
{
return toString(zipFile.getInputStream(zipEntry));
}
}
throw new IllegalStateException("Manifest not found");
}
private static String toString(InputStream inputStream) throws IOException
{
StringBuilder stringBuilder = new StringBuilder();
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)))
{
String line;
while ((line = bufferedReader.readLine()) != null)
{
stringBuilder.append(line);
stringBuilder.append(System.lineSeparator());
}
}
return stringBuilder.toString().trim() + System.lineSeparator();
}
Despite the flexibility, for just reading data this answer is the best.
I would suggest to make following:
Package aPackage = MyClassName.class.getPackage();
String implementationVersion = aPackage.getImplementationVersion();
String implementationVendor = aPackage.getImplementationVendor();
Where MyClassName can be any class from your application written by you.
You could use something like this:
public static String getManifestInfo() {
Enumeration resEnum;
try {
resEnum = Thread.currentThread().getContextClassLoader().getResources(JarFile.MANIFEST_NAME);
while (resEnum.hasMoreElements()) {
try {
URL url = (URL)resEnum.nextElement();
InputStream is = url.openStream();
if (is != null) {
Manifest manifest = new Manifest(is);
Attributes mainAttribs = manifest.getMainAttributes();
String version = mainAttribs.getValue("Implementation-Version");
if(version != null) {
return version;
}
}
}
catch (Exception e) {
// Silently ignore wrong manifests on classpath?
}
}
} catch (IOException e1) {
// Silently ignore wrong manifests on classpath?
}
return null;
}
To get the manifest attributes, you could iterate over the variable "mainAttribs" or directly retrieve your required attribute if you know the key.
This code loops through every jar on the classpath and reads the MANIFEST of each. If you know the name of the jar you may want to only look at the URL if it contains() the name of the jar you are interested in.