how to check the version of jar file?
Asked Answered
D

17

126

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?

Dispersant answered 29/4, 2011 at 16:36 Comment(0)
A
108

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).

Astragalus answered 29/4, 2011 at 16:42 Comment(9)
As i told that I am working on enhancing the application, I have downloaded the jar files and I know the version of the jar files. But I wanted to know the version of the jar which the package belongs to, I hope you get it.Dispersant
To explain you more, I have import x.y.z, I know that x.y belongs to a-4.1.jar, but the application which i am working has been developed long back, and I dono what version of a.jar file have they used, I am worried about the version because, some of the classes have been depreciated from the jar of the older version (I feel so), because, even though I have jar in the library, I find the import cannot be resolvedDispersant
So is it that, if you know the version of jar which they used while building the application, you would also use the same version of jar to execute the application?Astragalus
If that is what your requirement is, then am afraid that you will have to rebuild the application with exclusion of deprecated jars. Because finding version number associated with a given jar is like one to one function. Only one version number for one jar file. But finding which version of jar was used at the time of development of application sounds highly impossible to me unless the developer attaches the required jars with the application.Astragalus
The problem is I dono what version they used, SO i guess I need to work out using every version of the jar file... lets see if I can find any solution.Dispersant
Ya, that is what u can do. U need to check with different versions of jar files and wait till you get lucky. :)Astragalus
This answer (and others) is currently discussed on MetaWhistler
Recording version in MANIFEST.MF appears to be optional. There's no version recorded in various sqljdbc42.jar files that I've used with Cognos, yet Cognos is able to report a version (4.2.6420.100). Where is it getting this version from if it's not recorded in the manifest?Sociability
Thank you! This is working for me: unzip -p /usr/share/jenkins/agent.jar META-INF/MANIFEST.MF | awk '$1 == "Version:"{print $2}' | sed 's/\r//'Bancroft
G
80

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
Garbage answered 11/7, 2016 at 18:11 Comment(0)
W
41

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

Wentzel answered 2/5, 2014 at 20:7 Comment(1)
This answer (and others) is currently discussed on MetaWhistler
M
19

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.

Mulberry answered 22/12, 2011 at 16:23 Comment(1)
This answer (and others) is currently discussed on MetaWhistler
F
14

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: What is the best way to calculate a checksum for a file that is on my machine?

Fill answered 4/4, 2013 at 15:59 Comment(1)
I just calculated the jar md5 and pasted it to google. Worked great, thanks a lot!Soutane
N
12

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

Natant answered 18/1, 2017 at 17:7 Comment(0)
S
10

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
Simulant answered 24/12, 2019 at 0:51 Comment(0)
O
4

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/

Orometer answered 10/12, 2015 at 10:20 Comment(2)
Thanks for the reference to javaxt, I just used the simpler code sample there from javaxt.com/Tutorials/Jar/…Terracotta
FYI, for those who prefer maven to pull javaxt-core rather than downloading the JAR, you could do either of these approaches: gist.github.com/daluu/dda7b29cb5b6e0fbfbaec664eb759739, gist.github.com/daluu/52af7eef52563ddf78feTerracotta
P
2

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.

Pseudohemophilia answered 19/10, 2017 at 13:54 Comment(0)
H
2

For Linux, try following:

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

Hughes answered 9/8, 2019 at 16:11 Comment(1)
zipgrep "Implementation-Version:" JAVA.jar | awk -F ': ' '{print $2}'Zaffer
F
2

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.

Frontlet answered 7/10, 2019 at 15:4 Comment(0)
T
2

[A] Manually we can extract/unzip the jar file and check Bundle-Version or Implementation-Version keyword in META-INF/MANIFEST.MF file.

[B] In Unix environment , you can easily use the grep command in combination with unzip and then cut command to get the version. [This soluction does assume that there will be version mentioned in META-INF/MANIFEST.MF . (80% chances)]

Run below command to get the VERSION.

VERSION=unzip -p "javaPackage.jar" "META-INF/MANIFEST.MF" | grep -m1 -E "Bundle-Version|Implementation-Version:" | cut -d':' -f2 | awk '{$1=$1;print}' 2>/dev/null;

Explanation :

  1. unzip -> Get content of META-INF/MANIFEST.MF file from your java jar, This can be done without extracting the jar.
  2. grep -> Search matching word against either "Bundle-Version" or "Implementation-Version:" keyword
  3. cut -> Split based on hyphen and get 2nd part
  4. awk -> Trim space present around the jar-version
  5. 2>/dev/null -> Error redirection to /dev/null

Note : - If you want to find version of multiple jars , then run a for loop and call above script.

Totten answered 20/6, 2023 at 21:7 Comment(0)
A
1

You can filter version from the MANIFEST file using

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

Anemone answered 3/10, 2018 at 21:50 Comment(1)
This seems to mostly duplicate this older answer - https://mcmap.net/q/174691/-how-to-check-the-version-of-jar-file - and manifests are not guaranteed to have Bundle-Version.Mohandis
W
1

best solution that does not involve extracting the jar files is to run the following command. If the jar file does not contain a manifest file you will get a "WARNING: Manifest file not found"

java -jar file.jar -v

Warn answered 25/1, 2023 at 8:0 Comment(0)
D
0

You could execute this script, it will find all versions of jar files in a directory:

find /directory -maxdepth 3 -name "*.jar" | xargs -l % sh -c "JAR=\"\$(basename % .jar)"; VERSION=\"\$(unzip -q -c % META-INF/MANIFEST.MF | grep 'Implementation-Version' cut -d' ' -f2)\"; echo \"\$JAR \$VERSION\";" | sort
Dragonet answered 21/4, 2023 at 22:27 Comment(0)
R
-2

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.

Rene answered 31/3, 2021 at 15:25 Comment(1)
You are basically copying the accepted answer.Kristoferkristoffer
E
-17

It can be checked with a command java -jar jarname

Entremets answered 22/5, 2015 at 7:26 Comment(1)
This will run the main() method of the JARWhistler

© 2022 - 2024 — McMap. All rights reserved.