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?
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).
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 unzip -p /usr/share/jenkins/agent.jar META-INF/MANIFEST.MF | awk '$1 == "Version:"{print $2}' | sed 's/\r//'
–
Bancroft 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
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
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.
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?
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
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
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/
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.
For Linux, try following:
find . -name "YOUR_JAR_FILE.jar" -exec zipgrep "Implementation-Version:" '{}' \;|awk -F ': ' '{print $2}'
zipgrep "Implementation-Version:" JAVA.jar | awk -F ': ' '{print $2}'
–
Zaffer 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.
[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 :
- unzip -> Get content of META-INF/MANIFEST.MF file from your java jar, This can be done without extracting the jar.
- grep -> Search matching word against either "Bundle-Version" or "Implementation-Version:" keyword
- cut -> Split based on hyphen and get 2nd part
- awk -> Trim space present around the jar-version
- 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.
You can filter version from the MANIFEST file using
unzip -p my.jar META-INF/MANIFEST.MF | grep 'Bundle-Version'
Bundle-Version
. –
Mohandis 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
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
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.
It can be checked with a command java -jar jarname
main()
method of the JAR –
Whistler © 2022 - 2024 — McMap. All rights reserved.