gpt4 book ai didi

how to check the version of jar file?(如何查看JAR文件的版本?)

转载 作者:bug小助手 更新时间:2023-10-25 21:47:06 24 4
gpt4 key购买 nike



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?

我目前正在开发一个J2ME波兰语应用程序,只是对其进行了增强。我发现很难获得JAR文件的准确版本。有没有办法为类中完成的导入找到JAR文件的版本?我的意思是,如果您有什么东西,导入x.y.z;我们可以知道JAR X.Y包的版本吗?


更多回答
优秀答案推荐

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

解压缩JAR文件并查找清单文件(META-INF\MANIFEST.MF)。JAR文件的清单文件可能包含版本号(但并不总是指定版本)。



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

您需要将其解压缩并检查其META-INF/MANIFEST.MF文件,例如



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!

在JAR中的META-INF/MANIFEST.MF文件中,您可能会看到一行:MANIFEST-VERSION:1.0JAR←这不是JAR版本号!



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

您需要查找Implementation-Version,如果存在的话,它是一个自由文本字符串,因此完全取决于您在其中找到的内容。另请参阅Oracle文档和包版本规范



Just to complete the above answer.

只是为了完成上面的答案。



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

清单文件位于JAR内的META-INF\MANIFEST.MF路径下。



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

您可以在任何支持Zip的归档程序中检查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.

每个JAR版本都有一个唯一的校验和。您可以计算JAR(没有版本信息)的校验和,并将其与JAR的不同版本进行比较。我们还可以使用校验和来搜索JAR。



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.

基本上,您应该使用java.lang.Package类,它使用类加载器为您提供有关类的信息。



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.

我认为众所周知,Logback使用此功能来跟踪其生成的堆栈跟踪中每个类的JAR名称/版本。



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

另请参阅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:

检查CLi版本:



Run the following on the CLi jar file:

对CLI JAR文件运行以下命令:



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.

上面列出了CLI版本。



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

这个简单的程序将列出JAR版本的所有情况,即




  • 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/

它使用的是可以从http://www.javaxt.com/downloads/下载的Java-core 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

这些方法允许我访问JAR属性。我喜欢向后兼容,并使用最新的。所以我用了这个



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.

当您使用maven并且需要已知类的pom细节时,这很有效。希望这能帮上忙。



For Linux, try following:

对于Linux,请尝试以下操作:



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

找到。-name“Your_JAR_FILE.jar”-exec zipgrep“Implementation-Version:”‘{}’\;|awk-F‘:’‘{print$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).

如果你有WinRar,用WinRar打开JAR,双击打开META-INF文件夹。解压缩MANIFEST.MF并将文件更改到任何位置(例如桌面)。



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

在文本编辑器中打开解压缩文件:您将看到Implementation-Version或Release Version。



You can filter version from the MANIFEST file using

您可以使用以下命令从清单文件中筛选版本



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

解压缩-p my.jar META-INF/MANIFEST.MF|grep‘Bundle-Version’



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"

不需要解压缩JAR文件的最佳解决方案是运行以下命令。如果JAR文件不包含清单文件,您将收到“警告:未找到清单文件”


java -jar file.jar -v

JAVA-JAR文件.jar-v



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

您可以执行此脚本,它将在一个目录中找到所有版本的JAR文件:


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


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

[A]我们可以手动解压/解压缩JAR文件,并检查META-INF/MANIFEST.MF文件中的Bundle-Version或Implementation-Version关键字。


[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)]

[B]在Unix环境下,你可以很容易地使用grep命令与unzip命令结合,然后再使用cut命令来获取版本。[This solvent确实假定在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;

VERSION=unzip -p“javaPackage.jar”“META-INF/MANIFEST.MF”|grep -m1 -E“Bundle-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.

注意:-如果你想找到多个JAR的版本,那么运行一个for循环并调用上面的脚本。



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.

只需将扩展名重命名为.zip,而不是.jar。然后转到META-INF/MANIFEST.MF并使用记事本打开MANIFEST.MF文件。您可以在那里找到实现版本。



It can be checked with a command java -jar jarname

可以使用命令Java-jar jarname进行检查


更多回答

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.

正如我所说的,我正在致力于增强应用程序,我已经下载了JAR文件,并且我知道JAR文件的版本。但我想知道包属于哪个版本的JAR,我希望你能得到它。

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 resolved

更详细地说,我导入了x.y.z,我知道X.Y属于a-4.1.jar,但我正在工作的应用程序是很早以前开发的,而且我不知道他们使用的.jar文件的版本是什么,我担心版本,因为,一些类已经从旧版本的JAR中折旧了(我感觉是这样),因为,即使我的库中有JAR,我发现导入也无法解决

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?

那么,如果您知道他们在构建应用程序时使用的JAR版本,您是否也会使用相同版本的JAR来执行应用程序?

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.

如果这就是您的要求,那么恐怕您将不得不重新构建应用程序,并排除不推荐使用的JAR。因为查找与给定JAR相关联的版本号类似于一对一函数。一个JAR文件只有一个版本号。但是,除非开发人员将所需的JAR附加到应用程序中,否则找出在开发应用程序时使用了哪个版本的JAR对我来说是非常不可能的。

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?

MANIFEST.MF中的录制版本似乎是可选的。在我与Cognos一起使用的各种sqljdbc42.jar文件中没有记录版本,但Cognos能够报告版本(4.2.6420.100)。如果没有记录在清单中,它是从哪里得到这个版本的?

This answer (and others) is currently discussed on Meta

这个答案(和其他答案)目前正在Meta上讨论

This answer (and others) is currently discussed on Meta

这个答案(和其他答案)目前正在Meta上讨论

I just calculated the jar md5 and pasted it to google. Worked great, thanks a lot!

我刚刚计算了jar md5并将其粘贴到谷歌。工作得很好,非常感谢!

Thanks for the reference to javaxt, I just used the simpler code sample there from javaxt.com/Tutorials/Jar/…

感谢参考了javaxt,我只使用了来自javaxt.com/tutorials/jar/…的更简单的代码样例

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/52af7eef52563ddf78fe

仅供参考,对于那些喜欢使用Maven而不是下载JAR的人来说,您可以使用以下两种方法之一:gist.github.com/daluu/dda7b29cb5b6e0fbfbaec664eb759739,gist.githorb.com/daluu/52af7eef52563ddf78fe

zipgrep "Implementation-Version:" JAVA.jar | awk -F ': ' '{print $2}'

Zipgrep“Implementation-Version:”JAVA.jar|awk-F‘:’‘{print$2}’

This seems to mostly duplicate this older answer - stackoverflow.com/a/38313502/272387 - and manifests are not guaranteed to have Bundle-Version.

这似乎主要复制了这个较旧的答案--Stackoverflow.com/a/38313502/272387--而且清单不能保证有捆绑版本。

You are basically copying the accepted answer.

你基本上是在抄袭公认的答案。

This will run the main() method of the JAR

这将运行JAR的main()方法

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com