how to check the version of jar file?

JarVersionExecutable Jar

Jar Problem Overview


I am currently working on a J2ME polish application, just enhancing it. I am finding difficulties to get the exact version of the jar file. Is there any way to find the version of the jar file for the imports done in the class? I mean if you have some thing, import x.y.z; can we know the version of the jar x.y package belongs to?

Jar Solutions


Solution 1 - Jar

Decompress the JAR file and look for the manifest file (META-INF\MANIFEST.MF). The manifest file of JAR file might contain a version number (but not always a version is specified).

Solution 2 - Jar

You need to unzip it and check its META-INF/MANIFEST.MF file, e.g.

unzip -p file.jar | head

or more specific:

unzip -p file.jar META-INF/MANIFEST.MF

Solution 3 - Jar

Just to expand on the answers above, inside the META-INF/MANIFEST.MF file in the JAR, you will likely see a line: Manifest-Version: 1.0 ← This is NOT the jar versions number!

You need to look for Implementation-Version which, if present, is a free-text string so entirely up to the JAR's author as to what you'll find in there. See also Oracle docs and Package Version specificaion

Solution 4 - Jar

Just to complete the above answer.

Manifest file is located inside jar at META-INF\MANIFEST.MF path.

You can examine jar's contents in any archiver that supports zip.

Solution 5 - Jar

Each jar version has a unique checksum. You can calculate the checksum for you jar (that had no version info) and compare it with the different versions of the jar. We can also search a jar using checksum.

Refer this Question to calculate checksum: <https://stackoverflow.com/questions/478722/what-is-the-best-way-to-calculate-a-checksum-for-a-file-that-is-on-my-machine>

Solution 6 - Jar

Basically you should use the java.lang.Package class which use the classloader to give you informations about your classes.

example:

String.class.getPackage().getImplementationVersion();
Package.getPackage(this).getImplementationVersion();
Package.getPackage("java.lang.String").getImplementationVersion();

I think logback is known to use this feature to trace the JAR name/version of each class in its produced stacktraces.

see also http://docs.oracle.com/javase/8/docs/technotes/guides/versioning/spec/versioning2.html#wp90779

Solution 7 - Jar

Thought I would give a more recent answer as this question still comes up pretty high on searches.

Checking CLi JAR Version:

Run the following on the CLi jar file:

unzip -p jenkins-cli.jar META-INF/MANIFEST.MF

Example Output:

Manifest-Version: 1.0
Built-By: kohsuke
Jenkins-CLI-Version: 2.210  <--- Jenkins CLI Version
Created-By: Apache Maven 3.6.1
Build-Jdk: 1.8.0_144
Main-Class: hudson.cli.CLI

The CLi version is listed above.

To get the Server Version, run the following:

java -jar ./jenkins-cli.jar -s https://<Server_URL> -auth <email>@<domain>.com:<API Token> version

(the above will vary based on your implementation of authentication, please change accordingly)

Example Output:

Dec 23, 2019 4:42:55 PM org.apache.sshd.common.util.security.AbstractSecurityProviderRegistrar getOrCreateProvider
INFO: getOrCreateProvider(EdDSA) created instance of net.i2p.crypto.eddsa.EdDSASecurityProvider
2.210  <-- Jenkins Server Version

Solution 8 - Jar

This simple program will list all the cases for version of jar namely

  • Version found in Manifest file

  • No version found in Manifest and even from jar name

  • Manifest file not found

     Map<String, String> jarsWithVersionFound   = new LinkedHashMap<String, String>();
     List<String> jarsWithNoManifest     = new LinkedList<String>();
     List<String> jarsWithNoVersionFound = new LinkedList<String>();
     
     //loop through the files in lib folder
     //pick a jar one by one and getVersion()
     //print in console..save to file(?)..maybe later
     
     File[] files = new File("path_to_jar_folder").listFiles();
     
     for(File file : files)
     {
     	String fileName = file.getName();
     	
     	
     	try
     	{
     		String jarVersion = new Jar(file).getVersion();
     		
     		if(jarVersion == null)
     			jarsWithNoVersionFound.add(fileName);
     		else
     			jarsWithVersionFound.put(fileName, jarVersion);
    
     	}
     	catch(Exception ex)
     	{
     		jarsWithNoManifest.add(fileName);
     	}
     }
     
     System.out.println("******* JARs with versions found *******");
     for(Entry<String, String> jarName : jarsWithVersionFound.entrySet())
     	System.out.println(jarName.getKey() + " : " + jarName.getValue());
     
     System.out.println("\n \n ******* JARs with no versions found *******");
     for(String jarName : jarsWithNoVersionFound)
     	System.out.println(jarName);
    
     System.out.println("\n \n ******* JARs with no manifest found *******");
     for(String jarName : jarsWithNoManifest)
     	System.out.println(jarName);
    

It uses the javaxt-core jar which can be downloaded from http://www.javaxt.com/downloads/

Solution 9 - Jar

I'm late this but you can try the following two methods

using these needed classes

import java.util.jar.Attributes;
import java.util.jar.Manifest;

These methods let me access the jar attributes. I like being backwards compatible and use the latest. So I used this

public Attributes detectClassBuildInfoAttributes(Class sourceClass) throws MalformedURLException, IOException {
	String className = sourceClass.getSimpleName() + ".class";
	String classPath = sourceClass.getResource(className).toString();
	if (!classPath.startsWith("jar")) {
	  // Class not from JAR
	  return null;
	}
	String manifestPath = classPath.substring(0, classPath.lastIndexOf("!") + 1) + 
	    "/META-INF/MANIFEST.MF";
	Manifest manifest = new Manifest(new URL(manifestPath).openStream());
	return manifest.getEntries().get("Build-Info");
}

public String retrieveClassInfoAttribute(Class sourceClass, String attributeName) throws MalformedURLException, IOException {
	Attributes version_attr = detectClassBuildInfoAttributes(sourceClass);
	
	String attribute = version_attr.getValue(attributeName);
	
	return attribute;
}

This works well when you are using maven and need pom details for known classes. Hope this helps.

Solution 10 - Jar

For Linux, try following:

find . -name "YOUR_JAR_FILE.jar" -exec zipgrep "Implementation-Version:" '{}' ;|awk -F ': ' '{print $2}'

Solution 11 - Jar

If you have winrar, open the jar with winrar, double-click to open folder META-INF. Extract MANIFEST.MF and CHANGES files to any location (say desktop).

Open the extracted files in a text editor: You will see Implementation-Version or release version.

Solution 12 - Jar

You can filter version from the MANIFEST file using

unzip -p my.jar META-INF/MANIFEST.MF | grep 'Bundle-Version'

Solution 13 - Jar

Just rename the extension with .zip instead of .jar. Then go to META-INF/MANIFEST.MF and open the MANIFEST.MF file with notepad. You can find the implementation version there.

Solution 14 - Jar

It can be checked with a command java -jar jarname

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRitesh MengjiView Question on Stackoverflow
Solution 1 - JarVivekView Answer on Stackoverflow
Solution 2 - JarkenorbView Answer on Stackoverflow
Solution 3 - JarjothView Answer on Stackoverflow
Solution 4 - JarVadzimView Answer on Stackoverflow
Solution 5 - JarvijayView Answer on Stackoverflow
Solution 6 - JarDonatelloView Answer on Stackoverflow
Solution 7 - JarDC.SkellsView Answer on Stackoverflow
Solution 8 - JarShanky_GuptaView Answer on Stackoverflow
Solution 9 - JarGetBackerZView Answer on Stackoverflow
Solution 10 - JarBae Cheol ShinView Answer on Stackoverflow
Solution 11 - JarGanesan RView Answer on Stackoverflow
Solution 12 - JarSudheer AvulaView Answer on Stackoverflow
Solution 13 - JarSuman DasView Answer on Stackoverflow
Solution 14 - JarAkash ToliaView Answer on Stackoverflow